Hibernate @Id @GenerateValue 注释无法识别 DB2 数据库生成的增量 ID
使用 Hibernate 3 将第二条记录插入到 DB2 v9.x 中,其中列具有 DB2 生成的唯一主键列 ID,这对于持久化 Hibernate 实体不起作用。
插入第一条记录工作正常,但是除非我使用设置器手动递增并设置值,否则无法保留更多记录。我尝试注释掉@GenerateValue...我是否正确使用了它?我不希望 Hibernate 自动递增 Id 列,我希望它使用 DB2 本身生成的递增 ID 来插入下一行。谢谢:)
这是我的代码:
private short APLSEQNUM;
@javax.persistence.Column(name = "MRT_APLSEQNUM")
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public short getAPLSEQNUM() {
return APLSEQNUM;
}
在 DB2 创建表脚本中添加的用于生成 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 )
这是例外:
休眠:插入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,M RT_APLYER、MRT_APLSEQNUM、MRT_ACMPNUM)值(? , ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 2011 年 3 月 15 日 17:37:44 org.hibernate.util.JDBCExceptionReporter logExceptions 警告:SQL 错误:-803,SQLState:23505 2011 年 3 月 15 日 17:37:44 org.hibernate.util.JDBCExceptionReporter logExceptions 严重:DB2 SQL 错误:SQLCODE:-803,SQLSTATE:23505,SQLERRMC:1;RPS.TMRT 2011 年 3 月 15 日 17:37:44 org.hibernate.event.def.AbstractFlushingEventListener PerformExecutions 严重:无法将数据库状态与会话同步 org.hibernate.exception.ConstraintViolationException:无法插入:[qa.gov.moi.rps.db.entity.Medical_MRT] 在 org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71) 在 org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) 在 org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2272) 在 org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2665) 在 org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:60) 在 org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279) 在 org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263) 在 org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167) 在org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) 在 org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) 在 org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) 在 org.hibernate.impl.SessionImpl.drivenFlush(SessionImpl.java:338) 在 org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106) 在 org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54) 在 qa.gov.moi.rps.db.helper.MedicalHelper.create(MedicalHelper.java:74) 在 qa.gov.moi.rps.db.helper.MedicalHelper.main(MedicalHelper.java:285) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method.invoke(Method.java:597) 在 com.intellij.rt.execution.application.AppMain.main(AppMain.java:115) 引起原因:com.ibm.db2.jcc.c.SqlException:DB2 SQL 错误:SQLCODE:-803,SQLSTATE:23505,SQLERRMC:1;RPS.TMRT 在 com.ibm.db2.jcc.c.fg.d(fg.java:1340) 在 com.ibm.db2.jcc.b.gb.k(gb.java:351) 在 com.ibm.db2.jcc.b.gb.a(gb.java:60) 在 com.ibm.db2.jcc.bwa(w.java:52) 在 com.ibm.db2.jcc.b.wb.c(wb.java:213) 在 com.ibm.db2.jcc.c.gg.ab(gg.java:1779) 在 com.ibm.db2.jcc.c.gg.d(gg.java:2324) 在 com.ibm.db2.jcc.c.gg.W(gg.java:457) 在 com.ibm.db2.jcc.c.gg.executeUpdate(gg.java:440) 在 org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23) 在 org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2252) ... 18 更多 javax.persistence.RollbackException:提交事务时出错 在 org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:71) 在 qa.gov.moi.rps.db.helper.MedicalHelper.create(MedicalHelper.java:74) 在 qa.gov.moi.rps.db.helper.MedicalHelper.main(MedicalHelper.java:285) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method.invoke(Method.java:597) 在 com.intellij.rt.execution.application.AppMain.main(AppMain.java:115) 导致:org.hibernate.exception.ConstraintViolationException:无法插入:[qa.gov.moi.rps.db.entity.Medical_MRT] 在org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71) 在 org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) 在 org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2272) 在 org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2665) 在 org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:60) 在 org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279) 在 org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263) 在 org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167) 在org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) 在 org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) 在 org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) 在 org.hibernate.impl.SessionImpl.drivenFlush(SessionImpl.java:338) 在 org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106) 在 org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54) ... 7 更多 引起原因:com.ibm.db2.jcc.c.SqlException:DB2 SQL 错误:SQLCODE:-803,SQLSTATE:23505,SQLERRMC:1;RPS.TMRT 在 com.ibm.db2.jcc.c.fg.d(fg.java:1340) 在 com.ibm.db2.jcc.b.gb.k(gb.java:351) 在 com.ibm.db2.jcc.b.gb.a(gb.java:60) 在 com.ibm.db2.jcc.bwa(w.java:52) 在 com.ibm.db2.jcc.b.wb.c(wb.java:213) 在 com.ibm.db2.jcc.c.gg.ab(gg.java:1779) 在 com.ibm.db2.jcc.c.gg.d(gg.java:2324) 在 com.ibm.db2.jcc.c.gg.W(gg.java:457) 在 com.ibm.db2.jcc.c.gg.executeUpdate(gg.java:440) 在 org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23) 在 org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2252) ... 18 更多
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用@GenerateValue(strategy = GenerationType.IDENTITY)
Use
@GeneratedValue(strategy = GenerationType.IDENTITY)
这就是我们所做的:
我们在 DB2 中创建了一个序列:
hibernate 映射文件中的
:在许多实例需要获取序列和序列的集群环境中工作生成的内容是唯一的。
here's what we did :
we created a sequence in DB2 :
in the hibernate mapping file :
Works in a clustered environment where many instances need to get a sequence and the sequence generated is unique.