-->
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: Problem when adding element to Session
PostPosted: Tue Jul 12, 2005 3:25 am 
Beginner
Beginner

Joined: Tue Jun 21, 2005 2:21 am
Posts: 22
Hi

I have two tables Parent and Child

PARENT CHILD
--------- --------
Id ChildID
Name Name
Parent_ID

I have a mapping file name person.hbm.xml which has mapping info
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name ="Parent" table="PARENT" >
<id name="id" column ="ID" >
<generator class="increment"/>
</id>
<property name = "name" />
<set name = "children" inverse="true" >
<key column="parent_id" />
<one-to-many class ="Child"/>
</set>
</class>

<class name ="Child" table="CHILD" >
<id name="id" column ="ID" >
<generator class="increment"/>
</id>
<property name ="name" />
<many-to-one name="parent" class="Parent" column="parent_id" cascade="all" />
</class>
</hibernate-mapping>

I have my own util class for loadning session info . I created one Person Manager.java file for my data population

Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Parent p = new Parent();
Child c = new Child();
p.addChild(c);
session.save(c);
tx.commit();

I have my concern in saving child record into session . If i change this to session.save(p) , data is inserted only in to Parent table . i hope this behaviour is because of having inverse tag in SET . ?

what changes in need to make in such a way if i want to do session.save(p) that will insert records into Parent as well as Child

thanks
balaji


Top
 Profile  
 
 Post subject: first u have use parent .setchaid(set) then
PostPosted: Tue Jul 12, 2005 8:04 am 
Newbie

Joined: Tue Jul 12, 2005 7:57 am
Posts: 2
irst u have use parent .setchaid(set) then,
after that u have to say session.save(parent)

for further help visit
http://www.systemmobile.com/articles/In ... te%20Works


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 12, 2005 8:26 am 
Beginner
Beginner

Joined: Tue Jun 21, 2005 2:21 am
Posts: 22
even if i set parent.getChildren.add(c) i'm facing the same problem .

in my mapping file i give cascade = all entry in both relation .
<set name = "children" inverse="true" cascade="all">
<key column="parent_id" not-null="true" />
<one-to-many class ="Child"/>
</set>

<many-to-one name="parent" class="Parent" column="parent_id" cascade="all"/>

but still i needs session.save(c). if i skip this ,all data are not getting inserted .

can anyone throw some light on cascade tag .

Thanks
Balaji


Top
 Profile  
 
 Post subject: i have seen your code,
PostPosted: Wed Jul 13, 2005 12:21 am 
Newbie

Joined: Tue Jul 12, 2005 7:57 am
Posts: 2
i have seen your code,
i have sended you one link u have refered the link,

http://www.systemmobile.com/articles/In ... 0Documents

in that he has given , good example which suited your need,
ok any how,

i have seen your code, u r adding the children one by one, first u have to add set of children to your parent, not one chidren, because, one entity, in the parent table have many entities in the chidren table, any how refer the link, all the best,
actually i also taking taining in hybernate,

good luck,

Regards
kiran kumar


Top
 Profile  
 
 Post subject: RE:Problem when adding element to Session
PostPosted: Wed Jul 13, 2005 12:59 am 
Newbie

Joined: Wed Jun 29, 2005 10:41 pm
Posts: 3
Location: New Jersey
Usually you want to set cascade from parent to child, not the other way around. So, add a cascade in the <one-to-many> tag in parent mapping file, and remove it from child's, will allow you to save a parent and automatically save a child. If you only update child, say change it's name, you just call save(c) parent will not (and should not) be impacted.

The inserve='true' is just preventing updating the fk in child table twice, not related to the cascade.

HTh.


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.