title: Primary key integrity constraint violation with Hibernate and HSQLDB
body: <p>Good morning everyone</p>
  
  <p>I am using HSQLDB with Hibernate. I have a situation where even if I set the primary key value, I am still getting an <code>integrity constraint violation: NOT NULL check constraint; SYS_CT_10346 table: NUMBERS column: PK</code>. As can be seen below.</p>
  
  <pre><code>SEVERE: integrity constraint violation: NOT NULL check constraint; SYS_CT_10346 table: NUMBERS column: PK
  org.hibernate.exception.ConstraintViolationException: could not insert: [entity.Numbers]
      at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
      at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
      at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
      at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
      at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
      at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
      at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
      at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
      at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
      at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
      at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
      at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
      at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
      at dbconn.NumbersDB.saveNumbers(NumbersDB.java:75)
      at gui.UI.saveNumberStateButtonActionPerformed(UI.java:1807)
      at gui.UI.access$1800(UI.java:39)
      at gui.UI$20.actionPerformed(UI.java:788)
      at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
      at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
      at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
      at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
      at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
      at java.awt.Component.processMouseEvent(Component.java:6505)
      at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
      at java.awt.Component.processEvent(Component.java:6270)
      at java.awt.Container.processEvent(Container.java:2229)
      at java.awt.Component.dispatchEventImpl(Component.java:4861)
      at java.awt.Container.dispatchEventImpl(Container.java:2287)
      at java.awt.Component.dispatchEvent(Component.java:4687)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
      at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
      at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
      at java.awt.Container.dispatchEventImpl(Container.java:2273)
      at java.awt.Window.dispatchEventImpl(Window.java:2719)
      at java.awt.Component.dispatchEvent(Component.java:4687)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
      at java.awt.EventQueue.access$200(EventQueue.java:103)
      at java.awt.EventQueue$3.run(EventQueue.java:688)
      at java.awt.EventQueue$3.run(EventQueue.java:686)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
      at java.awt.EventQueue$4.run(EventQueue.java:702)
      at java.awt.EventQueue$4.run(EventQueue.java:700)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
  Caused by: java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10346 table: NUMBERS column: PK
      at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
      at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
      at org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source)
      at org.hsqldb.jdbc.JDBCPreparedStatement.executeUpdate(Unknown Source)
      at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
      at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
      ... 55 more
  </code></pre>
  
  <p>Here is my mapping</p>
  
  <pre><code>&lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
  &lt;!-- Generated 22 Feb 2016 9:01:34 PM by Hibernate Tools 3.2.1.GA --&gt;
  &lt;hibernate-mapping&gt;
      &lt;class name="entity.Numbers" table="NUMBERS" schema="PUBLIC" catalog="PUBLIC"&gt;
          &lt;id name="pk" type="int"&gt;
              &lt;column name="PK" /&gt;
              &lt;generator class="native" /&gt;
          &lt;/id&gt;
          &lt;many-to-one name="account" class="entity.Account" fetch="select"&gt;
              &lt;column name="ALLOCATEDACCOUNT"/&gt;
          &lt;/many-to-one&gt;
          &lt;property name="number" type="string"&gt;
              &lt;column name="NUMBER" length="25" not-null="true" /&gt;
          &lt;/property&gt;
          &lt;property name="allocated" type="boolean"&gt;
              &lt;column name="ALLOCATED" not-null="true" /&gt;
          &lt;/property&gt;
          &lt;property name="alias" type="string"&gt;
              &lt;column name="ALIAS" length="25" /&gt;
          &lt;/property&gt;
          &lt;property name="geographic" type="java.lang.Boolean"&gt;
              &lt;column name="GEOGRAPHIC" /&gt;
          &lt;/property&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>And my entity class</p>
  
  <pre><code>package entity;
  // Generated 21 Feb 2016 1:44:55 AM by Hibernate Tools 3.2.1.GA
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.FetchType;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.ManyToOne;
  import javax.persistence.Table;
  
  /**
   * Numbers generated by hbm2java
   */
  @Entity
  @Table(name = "NUMBERS", schema = "PUBLIC", catalog = "PUBLIC")
  public class Numbers implements java.io.Serializable {
  
      private Integer pk;
      private String number;
      private Account account;
      private boolean allocated;
      private String alias;
      private Boolean geographic;
  
      public Numbers() {
      }
  
      public Numbers(Integer pk, String number, boolean allocated) {
          this.pk = pk;
          this.number = number;
          this.allocated = allocated;
      }
  
      public Numbers(Integer pk, Account account, String number, boolean allocated, String alias, Boolean geographic) {
          this.pk = pk;
          this.account = account;
          this.number = number;
          this.allocated = allocated;
          this.alias = alias;
          this.geographic = geographic;
      }
  
      @Id
      @Column(name = "PK", unique = true, nullable = false)    
      public Integer getPk() {
          return this.pk;
      }
  
      public void setPk(Integer pk) {
          this.pk = pk;
      }
  
      @Column(name = "NUMBER", nullable = false, length = 25)
      public String getNumber() {
          return this.number;
      }
  
      public void setNumber(String number) {
          this.number = number;
      }
  
      @Column(name = "ALLOCATED", nullable = false)
      public boolean isAllocated() {
          return this.allocated;
      }
  
      public void setAllocated(boolean allocated) {
          this.allocated = allocated;
      }
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "ALLOCATEDACCOUNT")
      public Account getAccount() {
          return this.account;
      }
  
      public void setAccount(Account account) {
          this.account = account;
      }
  
      @Column(name = "ALIAS", length = 25)
      public String getAlias() {
          return this.alias;
      }
  
      public void setAlias(String alias) {
          this.alias = alias;
      }
  
      @Column(name = "GEOGRAPHIC")
      public Boolean getGeographic() {
          return this.geographic;
      }
  
      public void setGeographic(Boolean geographic) {
          this.geographic = geographic;
      }
  }
  </code></pre>
  
  <p>and lastly the code that I am trying to execute</p>
  
  <pre><code>public boolean saveNumbers(Set numbersSet) {
          try {
              Session session = HibernateUtil.getSessionFactory().openSession();
              session.beginTransaction();
              for (Iterator&lt;Numbers&gt; it = numbersSet.iterator(); it.hasNext();) {
                  Numbers numbers = it.next();
                  if (numbers.getPk() == null) {
                      numbers.setPk(new Integer(5));
                      session.save(numbers);
                  } else {
                      numbers = (Numbers) session.merge(numbers);
                      session.saveOrUpdate(numbers);
                  }
              }
              session.getTransaction().commit();
              return true;
          } catch (HibernateException he) {
              he.printStackTrace();
              return false;
          }
      }
  </code></pre>
  
  <p>The exception is getting thrown by <code>session.save(numbers);</code></p>
  
  <p>Even when I set the primary key manually, as seen in the above sample it still throwing an exception.
  I have tried to set the annotations as seen below but this still gives me the same exception. </p>
  
  <pre><code>    @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "PK", unique = true, nullable = false)
  </code></pre>
  
  <p>your assistance would be greatly appreciated</p>
  
  <p>Thank you</p>