-->
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: Simple Question about one-to-many relationship
PostPosted: Mon Jan 12, 2004 6:54 am 
Beginner
Beginner

Joined: Thu Dec 04, 2003 3:47 am
Posts: 31
Location: Hong Kong
Sorry for some simple question

I have a parent object which is one-to-many to chidren object. The parent keeps the reference to its children in a Set. The children object keeps the reference to its parent. Both of them have not been saved to the database.


1. Suppose it is "save-update" cascade, when saving the parent to the database, the children object would also be saved. The foreign key in the record representing the children data object in the children table points to the record representing the parent data object in the parent table.
But how about I save the children data object? Would the parent data object be saved? or only the children data object is saved? (but the foriegn key would be null?)

2. Suppose the children data object has been destroyed. Would the parent knows about it? or if I try to access the children data object from the reference in the set of the parent data object, null pointer excpetion?

3. Suppose the children data object has been "deleted" from the database, that is making the persistent instance transient. Would the parent knows about it? or if I try to access the children data object from the reference in the set of the parent data object, I can still play with the children object, although its record in the database has been deleted?


Top
 Profile  
 
 Post subject: Re: Simple Question about one-to-many relationship
PostPosted: Mon Jan 12, 2004 8:58 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
ngkaka wrote:
1. Suppose it is "save-update" cascade, when saving the parent to the database, the children object would also be saved. The foreign key in the record representing the children data object in the children table points to the record representing the parent data object in the parent table.
But how about I save the children data object? Would the parent data object be saved? or only the children data object is saved? (but the foriegn key would be null?)

2. Suppose the children data object has been destroyed. Would the parent knows about it? or if I try to access the children data object from the reference in the set of the parent data object, null pointer excpetion?

3. Suppose the children data object has been "deleted" from the database, that is making the persistent instance transient. Would the parent knows about it? or if I try to access the children data object from the reference in the set of the parent data object, I can still play with the children object, although its record in the database has been deleted?


1. Parent data object will be saved if asked (<many-to-one cascade=save-update"/>)
2.<many-to-one> in the child object will tell hibernate to remove the link. However, if parent has been previously loaded, then you must manually update the parent collection in your java model. Have a look at the doc and at http://www.hibernate.org/Documentation/InsideExplanationOfInverseTrue

3. Be more specific

_________________
Emmanuel


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.