title: Hibernate: "Field 'id' doesn't have a default value"
body: <p>I'm facing what I think is a simple problem with Hibernate, but can't solve it (Hibernate forums being unreachable certainly doesn't help).</p>
  
  <p>I have a simple class I'd like to persist, but keep getting:</p>
  
  <pre><code>SEVERE: Field 'id' doesn't have a default value
  Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not insert: [hibtest.model.Mensagem]
      at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
      at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
      [ a bunch more ]
  Caused by: java.sql.SQLException: Field 'id' doesn't have a default value
      [ a bunch more ]
  </code></pre>
  
  <p>The relevant code for the persisted class is:</p>
  
  <pre><code>package hibtest.model;
  
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.Inheritance;
  import javax.persistence.InheritanceType;
  
  @Entity
  @Inheritance(strategy = InheritanceType.JOINED)
  public class Mensagem  {
      protected Long id;
  
      protected Mensagem() { }
  
      @Id
      @GeneratedValue
      public Long getId() {
          return id;
  }
  
      public Mensagem setId(Long id) {
          this.id = id;
          return this;
      }
  }
  </code></pre>
  
  <p>And the actual running code is just plain:</p>
  
  <pre><code>SessionFactory factory = new AnnotationConfiguration()
      .configure()
      .buildSessionFactory();
  
  {
      Session session = factory.openSession();
      Transaction tx = session.beginTransaction();
  
      Mensagem msg = new Mensagem("YARR!");
  
      session.save(msg);
  
      tx.commit();
      session.close();
  }
  </code></pre>
  
  <p>I tried some "strategies" within the <code>GeneratedValue</code> annotation but it just doesn't seem to work. Initializing <code>id</code> doesn't help either! (eg <code>Long id = 20L</code>).</p>
  
  <p>Could anyone shed some light?</p>
  
  <p><strong>EDIT 2:</strong> confirmed: messing with<code>@GeneratedValue(strategy = GenerationType.XXX)</code> doesn't solve it</p>
  
  <p><strong>SOLVED:</strong> recreating the database solved the problem</p>