-->
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: Some confusion regarding unidirectional one-to-many assoc.
PostPosted: Thu Jan 28, 2010 3:29 pm 
Newbie

Joined: Tue Jul 24, 2007 2:40 am
Posts: 6
In the section of hibernate reference mentioned below:

Quote:
Suppose we start with a simple <one-to-many> association from Parent to Child.
Code:
<set name="children">
<key column="parent_id"/>
<one-to-many class="Child"/>
</set>
If we were to execute the following code:
Code:
Parent p = .....;
Child c = new Child();
p.getChildren().add(c);
session.save(c);
session.flush();
Hibernate would issue two SQL statements:
• an INSERT to create the record for c
• an UPDATE to create the link from p to c
This is not only inefficient, but also violates any NOT NULL constraint on the parent_id column.
You can fix the nullability constraint violation by specifying not-null="true" in the collection
mapping:
Code:
<set name="children">
<key column="parent_id" not-null="true"/>
<one-to-many class="Child"/>
</set>
However, this is not the recommended solution.
The underlying cause of this behavior is that the link (the foreign key parent_id) from p to c is
not considered part of the state of the Child object and is therefore not created in the INSERT.
The solution is to make the link part of the Child mapping.
Code:
<many-to-one name="parent" column="parent_id" not-null="true"/>
You also need to add the parent property to the Child class.


The problem mentioned above should be true in any circumstances. Then there is no point of the use of unidirectional one-to-many association in circumstances. But there are still many instances of unidirectional association mapping in chapter 7. Why did not they mention that this is not recommended (in chapter 7 when they were discussing unidirectional stuffs)?


Top
 Profile  
 
 Post subject: Re: Some confusion regarding unidirectional one-to-many assoc.
PostPosted: Fri Jan 29, 2010 3:44 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
Why did not they mention that this is not recommended...


Hmmm.... it is mentioned... in section 7.2.3, the first place were were one-to-many is discussed:
Quote:
A unidirectional one-to-many association on a foreign key is an unusual case, and is not recommended.


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.