Inserting a second record using Hibernate 3 into DB2 v9.x where a column has a Unique primary key column Id generated by DB2 doesn't work with persisting my Hibernate entity.
Inserting the first record works fine, however unless I manually increment and set the value using the setter no more records can be persisted. I have tried commenting out @GeneratedValue....am I using this correctly? I do not want Hibernate to auto increment the Id column, I want it to use the incremented ID generated by DB2 itself to insert the next row. Thanks:)
This is my code:
private short APLSEQNUM;
@javax.persistence.Column(name = "MRT_APLSEQNUM")
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public short getAPLSEQNUM() {
return APLSEQNUM;
}
The contrainst added in DB2 create table script to generate the Id:
"MRT_APLSEQNUM" SMALLINT NOT NULL GENERATED BY DEFAULT AS IDENTITY ( START WITH +1
INCREMENT BY +1
MINVALUE +1
MAXVALUE +32767
NO CYCLE
CACHE 20
NO ORDER )
This is the exception:
Hibernate: insert into RPS.TMRT (MRT_APRNUM, MRT_CHKLOC, MRT_DPTCDE, MRT_FUNCDE, MRT_MDCNUM, MRT_MDCRSLCDE, MRT_MDCRSLDTE, MRT_OCCCDE, MRT_QIDNO, MRT_REGDTE, MRT_REMARK, MRT_SECCDE, MRT_SMP, MRT_USRID, MRT_VISNUM, MRT_APLYER, MRT_APLSEQNUM, MRT_ACMPNUM) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 15-Mar-2011 17:37:44 org.hibernate.util.JDBCExceptionReporter logExceptions WARNING: SQL Error: -803, SQLState: 23505 15-Mar-2011 17:37:44 org.hibernate.util.JDBCExceptionReporter logExceptions SEVERE: DB2 SQL error: SQLCODE: -803, SQLSTATE: 23505, SQLERRMC: 1;RPS.TMRT 15-Mar-2011 17:37:44 org.hibernate.event.def.AbstractFlushingEventListener performExecutions SEVERE: Could not synchronize database state with session org.hibernate.exception.ConstraintViolationException: could not insert: [qa.gov.moi.rps.db.entity.Medical_MRT] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2272) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2665) at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:60) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106) at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54) at qa.gov.moi.rps.db.helper.MedicalHelper.create(MedicalHelper.java:74) at qa.gov.moi.rps.db.helper.MedicalHelper.main(MedicalHelper.java:285) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115) Caused by: com.ibm.db2.jcc.c.SqlException: DB2 SQL error: SQLCODE: -803, SQLSTATE: 23505, SQLERRMC: 1;RPS.TMRT at com.ibm.db2.jcc.c.fg.d(fg.java:1340) at com.ibm.db2.jcc.b.gb.k(gb.java:351) at com.ibm.db2.jcc.b.gb.a(gb.java:60) at com.ibm.db2.jcc.b.w.a(w.java:52) at com.ibm.db2.jcc.b.wb.c(wb.java:213) at com.ibm.db2.jcc.c.gg.ab(gg.java:1779) at com.ibm.db2.jcc.c.gg.d(gg.java:2324) at com.ibm.db2.jcc.c.gg.W(gg.java:457) at com.ibm.db2.jcc.c.gg.executeUpdate(gg.java:440) at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2252) ... 18 more javax.persistence.RollbackException: Error while commiting the transaction at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:71) at qa.gov.moi.rps.db.helper.MedicalHelper.create(MedicalHelper.java:74) at qa.gov.moi.rps.db.helper.MedicalHelper.main(MedicalHelper.java:285) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115) Caused by: org.hibernate.exception.ConstraintViolationException: could not insert: [qa.gov.moi.rps.db.entity.Medical_MRT] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2272) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2665) at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:60) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106) at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54) ... 7 more Caused by: com.ibm.db2.jcc.c.SqlException: DB2 SQL error: SQLCODE: -803, SQLSTATE: 23505, SQLERRMC: 1;RPS.TMRT at com.ibm.db2.jcc.c.fg.d(fg.java:1340) at com.ibm.db2.jcc.b.gb.k(gb.java:351) at com.ibm.db2.jcc.b.gb.a(gb.java:60) at com.ibm.db2.jcc.b.w.a(w.java:52) at com.ibm.db2.jcc.b.wb.c(wb.java:213) at com.ibm.db2.jcc.c.gg.ab(gg.java:1779) at com.ibm.db2.jcc.c.gg.d(gg.java:2324) at com.ibm.db2.jcc.c.gg.W(gg.java:457) at com.ibm.db2.jcc.c.gg.executeUpdate(gg.java:440) at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2252) ... 18 more
Use @GeneratedValue(strategy = GenerationType.IDENTITY)