-->
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.  [ 2 posts ] 
Author Message
 Post subject: The usual parent/child problem (cannot persist children)...
PostPosted: Fri Aug 05, 2005 5:06 am 
Beginner
Beginner

Joined: Mon Nov 29, 2004 11:51 am
Posts: 31
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!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 05, 2005 5:22 am 
Beginner
Beginner

Joined: Mon Nov 29, 2004 11:51 am
Posts: 31
Somehow, posting a question usually leads me to resolve the problem immediately after posting. A 'delete post' button would be nice :)

Anyway, the issue, is that it looks like in a bidirectional relationship, you must explicitly assign the parent to the child (i.e. both directions).
So what I needed in that example I gave was this...
c1.setParent(p);
c2.setParent(p);

... and that solved the problem. It's shown in one (or more) of the documentation examples, but it somehow escaped me several times.


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

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.