Hello,
I know this is the most basic of functions/issues, but I've spent all night on it and am at a loss. I've read the doc and searched the forums. I'm really stuck.
Below, you'll see I've modeled a bi-directional relationship from Chapter 7 of the Hibernate documentation as best I could. In this example, all I want to do is create a Parent object, assign some Child objects to it as a collection and then persist the entire object graph with session.saveOrUpdate(parent). Whenever I try this, i get the follwing error:
org.hibernate.PropertyValueException: not-null property references a null or transient value: eg.test.Child.parent
I've tried every combination of various attributes, cascades, etc in the mapping file. Below is the mapping and code. Please help, it's killing me.
-------------------------------------------------------
Hibernate 3.0.5 MySQL 4.1
MAP: <class name="eg.test.Parent"> <id name="id"> <generator class="identity"/> </id> <set name="children" inverse="true" cascade="all"> <key column="parent_id"/> <one-to-many class="eg.test.Child"/> </set> </class>
<class name="eg.test.Child"> <id name="id"> <generator class="identity"/> </id> <property name="name"/> <many-to-one name="parent" class="eg.test.Parent" column="parent_id" not-null="true"/> </class>
CODE:
HashSet set = new HashSet(); Parent p = new Parent(); Child c1 = new Child(); c1.setName("a"); Child c2 = new Child(); c2.setName("b"); set.add(c1); set.add(c2); p.setChildren(set); HibernateUtil.getSession().saveOrUpdate(p);
STACK TRACE:
Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value: eg.test.Child.parent at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:236) at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:160) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:95) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:96) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69) at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:468) at org.hibernate.engine.Cascades$5.cascade(Cascades.java:154) at org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:771) at org.hibernate.engine.Cascades.cascade(Cascades.java:720) at org.hibernate.engine.Cascades.cascadeCollection(Cascades.java:895) at org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:792) at org.hibernate.engine.Cascades.cascade(Cascades.java:720) at org.hibernate.engine.Cascades.cascade(Cascades.java:847) at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:363) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:265) at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:160) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:95) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:96) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69) at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:468) at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:463) at eg.test.TestHibernate2.main(TestHibernate2.java:61)
CLASSES:
public class Parent { private long id; private Set children;
public long getId() { return id; } public void setId(long id) { this.id=id; }
public Set getChildren() { return children; } public void setChildren(Set children) { this.children=children; } }
public class Parent { private long id; private Set children;
public long getId() { return id; } public void setId(long id) { this.id=id; }
public Set getChildren() { return children; } public void setChildren(Set children) { this.children=children; } }
Thanks!
|