Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem when delete 1 record in ROOM table

 
Haifeng Jiang
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run my Hibernate application to delete one record in ROOM table, I got error. I suppose to delete 1 record in ROOM table then Hibernate will delete the related record in USER table. But actually it dose not work. Below are my codes. Hope anybody can give one hand. Thanks.
Test.java


User.hbm.xml:


Room.hbm.xml


Room.java:



User.java:


I got this problem when I runing Test.java. Does anyone know what's the problem? Many thanks!
20:25:27,484 DEBUG org.hibernate.jdbc.AbstractBatcher:418 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
20:25:27,484 DEBUG org.hibernate.util.JDBCExceptionReporter:92 - could not delete: [model.Room#2] [delete from JIANGHAIFENG.ROOM where ID=?]
com.ibm.db2.jcc.am.fo: DB2 SQL Error: SQLCODE=-532, SQLSTATE=23504, SQLERRMC=JIANGHAIFENG.USER.CC1318929856625, DRIVER=3.57.82
at com.ibm.db2.jcc.am.bd.a(bd.java:672)
at com.ibm.db2.jcc.am.bd.a(bd.java:60)
at com.ibm.db2.jcc.am.bd.a(bd.java:127)
at com.ibm.db2.jcc.am.km.b(km.java:2164)
at com.ibm.db2.jcc.am.km.c(km.java:2147)
at com.ibm.db2.jcc.t4.db.k(db.java:354)
at com.ibm.db2.jcc.t4.db.a(db.java:60)
at com.ibm.db2.jcc.t4.t.a(t.java:50)
at com.ibm.db2.jcc.t4.tb.b(tb.java:218)
at com.ibm.db2.jcc.am.lm.ec(lm.java:2692)
at com.ibm.db2.jcc.am.lm.b(lm.java:3552)
at com.ibm.db2.jcc.am.lm.Wb(lm.java:690)
at com.ibm.db2.jcc.am.lm.executeUpdate(lm.java:673)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:46)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2538)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2715)
at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:97)
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:172)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at test.Test.main(Test.java:145)
20:25:27,484 WARN org.hibernate.util.JDBCExceptionReporter:100 - SQL Error: -532, SQLState: 23504
20:25:27,484 ERROR org.hibernate.util.JDBCExceptionReporter:101 - DB2 SQL Error: SQLCODE=-532, SQLSTATE=23504, SQLERRMC=JIANGHAIFENG.USER.CC1318929856625, DRIVER=3.57.82
20:25:27,484 ERROR org.hibernate.event.def.AbstractFlushingEventListener:324 - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not delete: [model.Room#2]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2559)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2715)
at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:97)
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:172)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at test.Test.main(Test.java:145)
Caused by: com.ibm.db2.jcc.am.fo: DB2 SQL Error: SQLCODE=-532, SQLSTATE=23504, SQLERRMC=JIANGHAIFENG.USER.CC1318929856625, DRIVER=3.57.82
at com.ibm.db2.jcc.am.bd.a(bd.java:672)
at com.ibm.db2.jcc.am.bd.a(bd.java:60)
at com.ibm.db2.jcc.am.bd.a(bd.java:127)
at com.ibm.db2.jcc.am.km.b(km.java:2164)
at com.ibm.db2.jcc.am.km.c(km.java:2147)
at com.ibm.db2.jcc.t4.db.k(db.java:354)
at com.ibm.db2.jcc.t4.db.a(db.java:60)
at com.ibm.db2.jcc.t4.t.a(t.java:50)
at com.ibm.db2.jcc.t4.tb.b(tb.java:218)
at com.ibm.db2.jcc.am.lm.ec(lm.java:2692)
at com.ibm.db2.jcc.am.lm.b(lm.java:3552)
at com.ibm.db2.jcc.am.lm.Wb(lm.java:690)
at com.ibm.db2.jcc.am.lm.executeUpdate(lm.java:673)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:46)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2538)
... 11 more
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not delete: [model.Room#2]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2559)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2715)
at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:97)
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:172)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at test.Test.main(Test.java:145)
Caused by: com.ibm.db2.jcc.am.fo: DB2 SQL Error: SQLCODE=-532, SQLSTATE=23504, SQLERRMC=JIANGHAIFENG.USER.CC1318929856625, DRIVER=3.57.82
at com.ibm.db2.jcc.am.bd.a(bd.java:672)
at com.ibm.db2.jcc.am.bd.a(bd.java:60)
at com.ibm.db2.jcc.am.bd.a(bd.java:127)
at com.ibm.db2.jcc.am.km.b(km.java:2164)
at com.ibm.db2.jcc.am.km.c(km.java:2147)
at com.ibm.db2.jcc.t4.db.k(db.java:354)
at com.ibm.db2.jcc.t4.db.a(db.java:60)
at com.ibm.db2.jcc.t4.t.a(t.java:50)
at com.ibm.db2.jcc.t4.tb.b(tb.java:218)
at com.ibm.db2.jcc.am.lm.ec(lm.java:2692)
at com.ibm.db2.jcc.am.lm.b(lm.java:3552)
at com.ibm.db2.jcc.am.lm.Wb(lm.java:690)
at com.ibm.db2.jcc.am.lm.executeUpdate(lm.java:673)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:46)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2538)
... 11 more
 
Haifeng Jiang
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found if I get session in this way, the code works for me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic