Hibernate version:
3.2.2
Mapping documents:
The following is a rough example of the mapping:
<class name="Parent" table="PARENT">
<id name="id" column="id" type="long">
<generator class="identity"/>
</id>
<set name="mappings" lazy="true" inverse="true" cascade="all,delete-orphan">
<key column="SECONDARY_ID" not-null="true"/>
<one-to-many class="Mappings" not-found="exception"/>
</set>
</class>
<class name="Mappings" table="MAPPINGS">
<id name="id" column="id" type="long">
<generator class="identity"/>
</id>
<property name="primaryString" column="PRIMARYSTRING" type="string" not-null="false"/>
<property name="secondaryString" column="SECONDARYSTRING" type="string" not-null="false"/>
<many-to-one name="primaryContainer" column="PRIMARY_ID" not-null="true" fetch="join" lazy="false" cascade="persist,merge,save-update" class="Parent"/>
<many-to-one name="secondaryContainer" column="SECONDARY_ID" not-null="true" fetch="join" lazy="false" cascade="persist,merge,save-update" class="Parent"/>
</class>
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
org.hibernate.PropertyValueException: not-null property references a null or transient value: com.example.Mappings.primaryContainer
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
at org.hibernate.event.def.DefaultDeleteEventListener.deleteEntity(DefaultDeleteEventListener.java:250)
at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:141)
at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:775)
at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:758)
at org.hibernate.engine.CascadingAction$2.cascade(CascadingAction.java:121)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.DefaultDeleteEventListener.cascadeBeforeDelete(DefaultDeleteEventListener.java:307)
at org.hibernate.event.def.DefaultDeleteEventListener.deleteEntity(DefaultDeleteEventListener.java:246)
at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:141)
at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:775)
at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:758)
at org.hibernate.engine.CascadingAction$2.cascade(CascadingAction.java:121)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.DefaultDeleteEventListener.cascadeBeforeDelete(DefaultDeleteEventListener.java:307)
at org.hibernate.event.def.DefaultDeleteEventListener.deleteEntity(DefaultDeleteEventListener.java:246)
at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:141)
at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:52)
at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:766)
at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:744)
Name and version of the database you are using:
DB2 V9.13
First question: is the mapping specified valid? The key column in the Parent class mapping is for the SECONDARY_ID column, but there exists another many-to-one mapping in the Mappings class that is using a different column.
Loading the objects from the database works fine and I can see all the correct data. The exception is thrown when an attempt to delete the loaded object is made. It seems that the primary entry is loaded from one Hibernate query whilst the secondary entry is loaded through the one-to-many mapping.
Debugging into the checkNullability() method I can see that the Mappings object, for which the exception is thrown, has a null primaryContainer entry, which is invalid, hence the not-null exception. Changing the key column on the mapping to PRIMARY_ID reverses this i.e. the exception is thrown because secondaryContainer is null.
This exception doesn't happen when run against MS-SQL 2005.
Should the Parent class contain two seperate sets, one for the primary and one for the secondary, or is there another way to set up the mapping?
|