-->
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.  [ 3 posts ] 
Author Message
 Post subject: diff. behaviour from bag/set in Bidirectional one-to-many
PostPosted: Mon Jan 23, 2006 10:57 am 
Newbie

Joined: Mon Jan 23, 2006 10:06 am
Posts: 4
Hi guys
I'm using Hibernate version: 3.0.5. I'm using the version tag in all hbm.xml files. I have a question about chapter 21.2. Bidirectional one-to-many in the hibernate reference.

The Code:
Java:
Code:

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


Using a bag, only one SQL INSERT would be issued (What i've expected):
Parent:
Code:
   <bag name="children" inverse="true">
      <key column="PARENT_FK_ID"/>
      <one-to-many class="Child"/>
   </bag>

Child:
Code:
   <many-to-one name="parent" foreign-key="FK_CHILD_TO_PARENT" not-null="true">
      <column name="PARENT_FK_ID" length="40"/>
   </many-to-one>


Using a set, it's different behaviour. It is inserting the child and updating the Parent
Parent:
Code:
   <set name="children" inverse="true">
      <key column="PARENT_FK_ID"/>
      <one-to-many class="Child"/>
   </set>

Child:
Code:
   <many-to-one name="parent" foreign-key="FK_CHILD_TO_PARENT" not-null="true">
      <column name="PARENT_FK_ID" length="40"/>
   </many-to-one>


Question
Has anyone an idea why it's updating the parent in the set example?
How can I avoid the update on the parent?

TIA and best regards
Dominic


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 8:51 am 
Newbie

Joined: Mon Jan 23, 2006 10:06 am
Posts: 4
Hi
After upgrading to 3.1.2 the behaviour was similar on set and bag (An insert for the child and an update on the parent). I didn't see the bug in the release notes.

The update is because the version tag (modification counter).

It's possible to avoid the update on the parent, setting the optimistic-lock="false" E.G.:
Code:
   <set name="children" inverse="true" optimistic-lock="false">
      <key column="PARENT_FK_ID"/>
      <one-to-many class="Child"/>
   </set>


Best regards
Dominic


Top
 Profile  
 
 Post subject: The problem was HashCode
PostPosted: Mon Apr 16, 2007 3:17 am 
Newbie

Joined: Mon Jan 23, 2006 10:06 am
Posts: 4
Hi

The problem was the hascode for the set. Hiberate loads a proxy if hashcode is called. Therefore use set as often as required and not more.

Best regards
Dominic


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