-->
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: Parent-Child Relationship implementation
PostPosted: Fri Jun 25, 2004 6:16 pm 
Newbie

Joined: Fri Jun 04, 2004 7:22 pm
Posts: 8
Location: Phoenix, Arizona
Hi,
I would like to know the implementation for the following scenario.

Creating parent dynamically in the database and attach the children.


I have found the following code in hibernate site. Here, parent is already created and the child link is attached to it. But , my case is parent should be created in the database dynamically and then child needs to be attached. I am using sequence for generate the unique id for parent and child. My parent has only the field "id" which is generated dynamically. My child has other information with parent id as a foreign key.



<many-to-one name="parent" column="parent_id" not-null="true"/>
<set name="children" inverse="true">
<key column="parent_id"/>
<one-to-many class="Child"/>
</set>

Parent p = (Parent) session.load(Parent.class, pid);
Child c = new Child();
c.setParent(p);
p.getChildren().add(c);
session.save(c);
session.flush();


Please do the needful.

Thanks a ton in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 12:33 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Please, please read the doc

Code:
Parent p  = new Parent();
p.addChild(new Child()); //see the parent child relationship chapter
session.save(p);

<many-to-one name="parent" column="parent_id" not-null="true"/>
<set name="children" inverse="true" cascade="save-update">
<key column="parent_id"/>
<one-to-many class="Child"/>
</set>

_________________
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.