Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.0.5
Mapping documents:
Code:
<hibernate-mapping>
<class name="reverse.User" table="USER" schema="DB2INST1" lazy="false">
<composite-id name="id" class="reverse.UserId">
<key-property name="FirstName" type="java.lang.String">
<column name="FIRST_NAME" scale="20" precision="0" not-null="true" sql-type="VARCHAR" />
</key-property>
<key-property name="LastName" type="java.lang.String">
<column name="LAST_NAME" scale="20" precision="0" not-null="true" sql-type="VARCHAR" />
</key-property>
</composite-id>
<property name="BirthDate" type="java.util.Date">
<column name="BIRTH_DATE" scale="10" precision="0" not-null="true" sql-type="DATE" />
</property>
<set name="SetOfUserDetails" cascade="all">
<key>
<column name="FIRST_NAME" scale="20" precision="0" not-null="false" />
<column name="LAST_NAME" scale="20" precision="0" not-null="false" />
</key>
<one-to-many class="reverse.UserDetails" />
</set>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping>
<class name="reverse.UserDetails" table="USER_DETAILS" schema="DB2INST1" lazy="false">
<composite-id name="id" class="reverse.UserDetailsId">
<key-many-to-one name="User" class="reverse.User">
<column name="FIRST_NAME" scale="20" precision="0" not-null="false" />
<column name="LAST_NAME" scale="20" precision="0" not-null="false" />
</key-many-to-one>
<key-property name="UserId" type="java.lang.String">
<column name="USER_ID" scale="10" precision="0" not-null="true" sql-type="VARCHAR" />
</key-property>
</composite-id>
<property name="Group" type="java.lang.String">
<column name="GROUP" scale="20" precision="0" not-null="true" sql-type="VARCHAR" />
</property>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
UserId userId = new UserId();
userId.setFirstName("Robert");
userId.setLastName("Plant");
User user = (User) session.load(User.class, userId);
session.delete(user);
tx.commit();
Full stack trace of any exception that occurs:Code:
02:29:25,661 ERROR AbstractFlushingEventListener:277 - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not delete collection: [reverse.User.SetOfUserDetails#component[FirstName,LastName]{FirstName=Robert, LastName=Plant}]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:63)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.collection.AbstractCollectionPersister.remove(AbstractCollectionPersister.java:860)
at org.hibernate.action.CollectionRemoveAction.execute(CollectionRemoveAction.java:22)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:138)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at test.TestHibernate.main(TestHibernate.java:62)
Caused by: COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver][DB2/LINUX] SQL0407N Assignment of a NULL value to a NOT NULL column "FIRST_NAME" is not allowed. SQLSTATE=23502
at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(SQLExceptionGenerator.java:270)
at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(SQLExceptionGenerator.java:207)
at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.check_return_code(SQLExceptionGenerator.java:458)
at COM.ibm.db2.jdbc.app.DB2PreparedStatement.execute2(DB2PreparedStatement.java:2110)
at COM.ibm.db2.jdbc.app.DB2PreparedStatement.executeUpdate(DB2PreparedStatement.java:1642)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at org.hibernate.persister.collection.AbstractCollectionPersister.remove(AbstractCollectionPersister.java:850)
... 10 more
org.hibernate.exception.ConstraintViolationException: could not delete collection: [reverse.User.SetOfUserDetails#component[FirstName,LastName]{FirstName=Robert, LastName=Plant}]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:63)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.collection.AbstractCollectionPersister.remove(AbstractCollectionPersister.java:860)
at org.hibernate.action.CollectionRemoveAction.execute(CollectionRemoveAction.java:22)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:138)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at test.TestHibernate.main(TestHibernate.java:62)
Caused by: COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver][DB2/LINUX] SQL0407N Assignment of a NULL value to a NOT NULL column "FIRST_NAME" is not allowed. SQLSTATE=23502
at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(SQLExceptionGenerator.java:270)
at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(SQLExceptionGenerator.java:207)
at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.check_return_code(SQLExceptionGenerator.java:458)
at COM.ibm.db2.jdbc.app.DB2PreparedStatement.execute2(DB2PreparedStatement.java:2110)
at COM.ibm.db2.jdbc.app.DB2PreparedStatement.executeUpdate(DB2PreparedStatement.java:1642)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at org.hibernate.persister.collection.AbstractCollectionPersister.remove(AbstractCollectionPersister.java:850)
... 10 more
Name and version of the database you are using:DB2 V7
The generated SQL (show_sql=true):Code:
Hibernate: select user0_.FIRST_NAME as FIRST1_0_, user0_.LAST_NAME as LAST2_0_, user0_.BIRTH_DATE as BIRTH3_0_0_ from DB2INST1.USER user0_ where user0_.FIRST_NAME=? and user0_.LAST_NAME=?
Hibernate: select setofuserd0_.FIRST_NAME as FIRST1_1_, setofuserd0_.LAST_NAME as LAST2_1_, setofuserd0_.USER_ID as USER3_1_, setofuserd0_.FIRST_NAME as FIRST1_0_, setofuserd0_.LAST_NAME as LAST2_0_, setofuserd0_.USER_ID as USER3_0_, setofuserd0_.GROUP as GROUP1_0_ from DB2INST1.USER_DETAILS setofuserd0_ where setofuserd0_.FIRST_NAME=? and setofuserd0_.LAST_NAME=?
Hibernate: update DB2INST1.USER_DETAILS set FIRST_NAME=null, LAST_NAME=null where FIRST_NAME=? and LAST_NAME=?
The last UPDATE statement is what causes the problem, but I don't understand why it is being generated. Do I need to do anything else in my code in order to delete the whole object tree?
By the way, changing the code to this:
Code:
UserId userId = new UserId();
userId.setFirstName("Robert");
userId.setLastName("Plant");
User user = (User) session.load(User.class, userId);
user.getSetOfUserDetails().clear();
tx.commit();
gives me the same error.
What am I doing wrong?
Thank you for your help.