-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Multiple set mappings but one key column
PostPosted: Thu Jun 19, 2008 12:51 pm 
Newbie

Joined: Thu Oct 06, 2005 5:26 am
Posts: 17
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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.