title: JPA annotation for MS SQL Server 2008 R2 IDENTITY column
body: <p>I want to know proper JPA annotation setup to handle MS Server 2008 R2 IDENTITY column.</p>
  
  <p>MS Server 2008 R2 (RTM) doesn't support SEQUENCE, but the IDENTITY gives auto increment when I tested by raw sql.</p>
  
  <p>However, by JPA I can't make it work by any strategy (IDENTITY, AUTO, TABLE) on @GeneratedValue annotation and I'm not allowed to change DB schema, since it is others. I didn't try with SEQUENCE, because of there is no sequence.</p>
  
  <p><strong>DB Table Schema</strong></p>
  
  <pre><code>CREATE TABLE [dbo].[invoice_header](
      [invoice_id] [int] IDENTITY(1,1) NOT NULL,
      [ApprovalDate] [datetime] NULL,
      [ApproverUserName] [nvarchar](50) NULL,
   CONSTRAINT [PK_invoice_header] PRIMARY KEY CLUSTERED 
  (
      [invoice_id] ASC
  )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
  ) ON [PRIMARY]
  
  GO
  </code></pre>
  
  <p><strong>JPA Annotation</strong></p>
  
  <pre><code>@Entity
  @Table(name = "invoice_header")
  public class InvoiceHeader {
  
      @Id
      @GeneratedValue( strategy = GenerationType.IDENTITY )
      @Column(name="invoice_id")
      private Long invoiceId;
  </code></pre>
  
  <p><strong>Hibernate Error</strong></p>
  
  <pre><code>Hibernate: insert into tbl_acct_textura_invoice_header (invoice_id, ApprovalDate, ApproverUserName) values (null, ?, ?)
  2015-06-16 11:12:24.149 ERROR 7157 --- [tp1024240671-18] o.h.engine.jdbc.spi.SqlExceptionHelper   : DEFAULT or NULL are not allowed as explicit identity values.
  </code></pre>
  
  <p>It failed because of passing null on the Id. I could make HQL not include invoice_id column during insert by insertable=false and updatable=false on @Column annotation, but then hibernate wasn't happy about not including Id column like below. Any suggestion? FYI, I'm using spring-data JpaRepository</p>
  
  <p><strong>Hibernate Error when not include the id column during insert</strong></p>
  
  <pre><code>nested exception is org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): ...InvoiceHeader
  </code></pre>