title: How to auto generate a primary key using Hibernate and an H2 database
body: <p>I am using Spring 4 and have the following set up for my data model:</p>
  
  <pre><code>@Entity
  @Table(name ="InstanceData")
  public class InstanceData {
  
      private Long instanceDataId;
      private Long heapUsed; //in bytes
      private Long heapMax; //in bytes
  
  
      @Id
      @Column(name="InstanceDataId")
      @SequenceGenerator(name="DataSeq", sequenceName="DATA_SEQ")
      @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="DataSeq")
      public Long getInstanceDataId() {
          return instanceDataId;
      }
  
      public void setInstanceDataId(Long id) {
          this.instanceDataId = id;
      }
  
      @Column(name="HeapUsed")
      public Long getHeapUsed() {
          return this.heapUsed;
      }
      public void setHeapUsed(Long heapUsed) {
          this.heapUsed = heapUsed;
      }
  
      @Column(name="HeapMax")
      public Long getHeapMax() {
          return this.heapMax;
      }
      public void setHeapMax(Long heapMax) {
          this.heapMax = heapMax;
      }
  </code></pre>
  
  <p>I let Hibernate create the schema automatically.  I then try the following SQL (on the H2 db):</p>
  
  <p>insert into instance_data (heap_used, heap_max) values (100, 100);</p>
  
  <p>The error I get is: Error: NULL not allowed for column "INSTANCE_DATA_ID"; 
  SQLState:  23502</p>
  
  <p>My question is why doesn't it auto generate the primary key?  How should I change my data model configuration so that the primary key is auto generated (starting at 1)?  Thanks for your help.</p>
  
  <p>I would like to understand why even though I am using the @GeneratedValue annotation, the primary key is not auto generated.</p>