title: IDENTITY_INSERT with JPA
body: <p>I am attempting to insert some fake data into an SQL Server Express database. I am using some simple code like this:</p>
  
  <pre><code>@Entity
  @Table(name = "People")
  public class Peeps implements Serializable {
  
      @Id
      @Column(name = "ID", columnDefinition = "Decimal(10,0)")
      private String id;
  
      @Column(name = "PERSON_NAME")
      private String name;
  
  }
  </code></pre>
  
  <p>I call the entity manager to create the above class as follows:</p>
  
  <pre><code>private EntityManager em = createManager();
  private EntityTransaction utx = em.getTransaction();
  
  final Peeps entity = new Peeps();
  entity.setId("10002");
  entity.setName("Joe");
  utx.begin();
  em.persist(entity);
  utx.commit();
  </code></pre>
  
  <p>However, when doing this, I get an error::</p>
  
  <pre><code> Cannot insert explicit value for identity column in table 'People' when IDENTITY_INSERT is set to OFF
  </code></pre>
  
  <p>So what I tried doing is something like this:</p>
  
  <pre><code>em.createNativeQuery("SET IDENTITY_INSERT People ON").executeUpdate();
  em.persist(entity);
  em.createNativeQuery("SET IDENTITY_INSERT People OFF").executeUpdate();
  </code></pre>
  
  <p>However, I still get the same error. My intuition leads me to believe that the connection is not being shared. Is there something I can do to instruct hibernate to set <strong>IDENTITY_INSERT</strong> to <strong>ON</strong> before I invoke my persistence?</p>