-->
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.  [ 7 posts ] 
Author Message
 Post subject: Parent Child relation generates update after insert
PostPosted: Mon Feb 15, 2010 12:00 pm 
Beginner
Beginner

Joined: Fri Jul 20, 2007 10:38 am
Posts: 49
hello,

I have a parent with a set of child elements (one-on-many). When I add a child to the parent and do saveOrUpdate with parent, the child is first inserted into the database and then updated to set the parentId in the child-table.
I can see the INSERT and UPDATE statements on the child in my logging.

Every child has a parent (not-null), and once it's set it never changes.
What am I missing that the insert-statement doesn't set the parentId immediately?

The parent has a one-to-many set to the child with key column set to not-null="false". I thought that ought to do it.

Thanks,
Lino


Top
 Profile  
 
 Post subject: Re: Parent Child relation generates update after insert
PostPosted: Mon Feb 15, 2010 1:24 pm 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:02 am
Posts: 48
I don't understand why you are using the term parent/child.


It is an one-to-many association between 2 entities.

you have to set the inverse="true"

<bag name="events" inverse="true".....
<key column="xxxxx"/>
<one-to-many class="xx.xx.xx" not-found="ignore"/>
</bag>


Top
 Profile  
 
 Post subject: Re: Parent Child relation generates update after insert
PostPosted: Mon Feb 15, 2010 4:10 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Read about the parent/child example in the Hibernate documentation: http://docs.jboss.org/hibernate/stable/ ... child.html

Setting inverse="true" is only part of the solution. You'll also need a <many-to-one> mapping from the child to the parent.


Top
 Profile  
 
 Post subject: Re: Parent Child relation generates update after insert
PostPosted: Tue Feb 16, 2010 12:16 am 
Newbie

Joined: Tue Jun 02, 2009 4:06 am
Posts: 16
Hi,

You need not do a saveOrUpdate explicitly to insert your collection/child object.

You use cascade=save-update in your .hbm file for the collection entity.By doing this you need not explicitly call saveOrUpdate.You retrieve your Parent/Main object and in the same session just set this Collection Object and close the transaction and session.Your collection object will be persisted.

inverse=true just indicates Hibernate that you need to avoid multiple insert on the rows which might result in error.It indicates hibernate that its the collection is the mirror image of the other.So any insert/update on one side will ignore the insert/update on other.
Read This article
http://suneelgv.wordpress.com/2008/11/2 ... y-to-many/

kartik


Top
 Profile  
 
 Post subject: Re: Parent Child relation generates update after insert
PostPosted: Tue Feb 16, 2010 5:47 am 
Beginner
Beginner

Joined: Fri Jul 20, 2007 10:38 am
Posts: 49
Ok, fixed it! thanks!


Top
 Profile  
 
 Post subject: Re: Parent Child relation generates update after insert
PostPosted: Tue Feb 16, 2010 6:38 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:02 am
Posts: 48
kravicha wrote:
Hi,

You need not do a saveOrUpdate explicitly to insert your collection/child object.

You use cascade=save-update in your .hbm file for the collection entity.By doing this you need not explicitly call saveOrUpdate.You retrieve your Parent/Main object and in the same session just set this Collection Object and close the transaction and session.Your collection object will be persisted.

inverse=true just indicates Hibernate that you need to avoid multiple insert on the rows which might result in error.It indicates hibernate that its the collection is the mirror image of the other.So any insert/update on one side will ignore the insert/update on other.
Read This article
http://suneelgv.wordpress.com/2008/11/2 ... y-to-many/

kartik


we can call saveOrUpdate on both parent and child. But with cascade options it is unnecessary. With cascade,saving the parent would save the collection as well. Even if you call saveOrUpdate(child) immediately after saveOrUpdate(parent), hibernate is not going to queue any sql statements for the second call.
For example,

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();

Child child= new Child();
child.setName("child1");// child has a hibernate generated surrogate key

List<Child > children= new ArrayList<Child >();
children.add(child);

Parent parent = new Parent (); //parent has a hibernate generated surrogate key as well
parent.setName("parent");
parent.setChildren(children);

child.setParent(parent );

session.saveOrUpdate(parent );
session.saveOrUpdate(theEvent);// This call is unnecessary with above cascade settings. Hibernate will not generate any sql for this
// but won't produce error because hibernate is going to ignore this.

session.getTransaction().commit();


So with cascade options you are simply saving the lines of code.

Inverse has a totally different purpose.
But inverse="true", you tell the foreign key should be inserted/updated by child.


Top
 Profile  
 
 Post subject: Re: Parent Child relation generates update after insert
PostPosted: Mon Apr 11, 2011 5:00 am 
Newbie

Joined: Sun Jan 23, 2011 4:14 am
Posts: 7
Lino Cibran wrote:
Ok, fixed it! thanks!


It will be help if you mention what you did, or which link you referred to, in resolving this issue.


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