-->
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.  [ 7 posts ] 
Author Message
 Post subject: Howto: Parent/Child (without using a Set)
PostPosted: Tue Oct 12, 2004 4:22 pm 
Regular
Regular

Joined: Fri Sep 03, 2004 2:01 pm
Posts: 51
Hibernate version:2.1.6

Im trying to map the following construct:

A (1..1) -- (0..1) B

property accessors + constructors omitted for clarity:

class A()
{
int id;
B b;
}

class B()
{
int id;
A a;
}


Any single 'A' could contain 0 or 1 'B'.
'B' cannot exist without an associated 'A'.

Ive been trying to use many-to-one and one-to-one, but when I try and save an 'A' I keep getting:

PropertyValueException : not-null property references a null or transient value: test.B.a

I have a simple main() that does this:

A a1 = new A();
B b = new B();
a1.setB( b );

A a2 = new A();

session.save( a1 );
session.save( a2 );

Since the lifetime of 'B' is controlled by 'A', I considered that A is the parent, B is the child in this mapping.

Can I map this using two tables, and not having a Set<B> in class A?



Nick


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 12, 2004 6:49 pm 
Regular
Regular

Joined: Fri Sep 03, 2004 2:01 pm
Posts: 51
Ok, ignore this, Ive figured it out - the mapping i had was fine - I was simply not setting the A on B and B on A.


Top
 Profile  
 
 Post subject: Parent Child mappings
PostPosted: Wed Oct 13, 2004 10:25 am 
Beginner
Beginner

Joined: Thu May 27, 2004 3:07 pm
Posts: 20
Can you send me your example? I'm struggling with the same thing, and cannot the save to cascade down to the child.


I create the parent, then the child, call the setter, and then call save,
and it will only insert the parent.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 13, 2004 10:31 am 
Regular
Regular

Joined: Fri Sep 03, 2004 2:01 pm
Posts: 51
A.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<!--$Id$ -->

<hibernate-mapping package="test">

<class name="A" table="A_rtab" >

<id name="id" type="long" column="id" unsaved-value="0" >
<generator class="native"/>
</id>

<one-to-one name="b"
property-ref="a"
cascade="save-update"
/>

</class>

</hibernate-mapping>


B.hbm.xml


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping package="test">

<class name="B" table="B_rtab" >

<id name="id" type="long" column="id" unsaved-value="0" >
<generator class="native"/>
</id>

<property name="name"/>

<many-to-one
name="a"
column="A_ID"
class="A"
not-null="true"
unique="true"
/>

</class>

</hibernate-mapping>




Dont forget to link both ways:

A a = new A(); // Parent
B b = new B(); // Child

a.setB( b );
b.setA( a );

session.save( a );

if you just do this:

A a = new A(); // Parent
B b = new B(); // Child

a.setB( b );

you will get a nont-null property execption when you attempt to cascade the save.

Nick


Top
 Profile  
 
 Post subject: Hmmm.....
PostPosted: Wed Oct 13, 2004 10:51 am 
Beginner
Beginner

Joined: Thu May 27, 2004 3:07 pm
Posts: 20
You're using a many-to-one in your mapping. I'm attempting to do a one-to-one in both directions, but can never get it to insert the child, unless i do it explicitly. The cascade doesn't seem to work , as it seems to be trying to insert the child first.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 13, 2004 10:53 am 
Regular
Regular

Joined: Fri Sep 03, 2004 2:01 pm
Posts: 51
Its not many to many, its:
(1..1) -- (0..1)

Notice the unique="true"

Nick


Top
 Profile  
 
 Post subject: Deleting old objects
PostPosted: Wed Oct 13, 2004 11:19 am 
Beginner
Beginner

Joined: Thu May 27, 2004 3:07 pm
Posts: 20
Now, my only problem is how to delete the old instance of B when you create a new B, and set it on A.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.