-->
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.  [ 5 posts ] 
Author Message
 Post subject: Simple parent/child (1-N) relationship
PostPosted: Tue Jun 05, 2007 7:49 am 
Newbie

Joined: Mon Jun 04, 2007 1:50 am
Posts: 4
Hello.

When I declare a simple one-to-many relationship, with unidirectional relation, each child insertion performing 2 SQL statements:

1) Insert the child record with all the fields except the foreign key columns.
2) Update the foreign key columns in the child record.

How can I avoid the second SQL statement ?

Thanks,
Yifat

<class name="Parent">
<id name="id" type="string" column="PARENT_ID">
<generator class="uuid" />
</id>
<property name="name" type="string" length="20" />
<set name="children" cascade="all">
<key column="PARENT_ID" />
<one-to-many class="Child" />
</set>
</class>

<class name="Child">
<id name="id" type="string" column="CHILD_ID">
<generator class="uuid" />
</id>
<property name="name" type="string" length="20" />
</class>

From the log file:

Hibernate: insert into YIFAT.Parent (name, PARENT_ID) values (?, ?)
Hibernate: insert into YIFAT.Child (name, CHILD_ID) values (?, ?)
Hibernate: update YIFAT.Child set PARENT_ID=? where CHILD_ID=?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 8:18 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

set inverse="true" in set element
Code:
<set name="children" inverse="true" cascade="all">



Amila

(Don't forget to rate if helps)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 8:30 am 
Newbie

Joined: Mon Jun 04, 2007 1:50 am
Posts: 4
Thank you for your answer.

Changing to inverse=true prevent the second update but the child records left without a value in the foreign key column and they are not connected to the parent record.

yifat


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 8:51 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

I can't understand situation

can you send sql

Amila


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 9:45 am 
Newbie

Joined: Mon Jun 04, 2007 1:50 am
Posts: 4
Hibernate: insert into YIFAT.Parent (name, PARENT_ID) values (?, ?)
Hibernate: insert into YIFAT.Child (name, CHILD_ID) values (?, ?)

As you can see, the PARENT_ID column is not handled in the Child record insertion and the child left orphan.


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