-->
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: Use of foreign generator with one-one associations
PostPosted: Mon Nov 17, 2003 5:14 pm 
Newbie

Joined: Fri Nov 14, 2003 9:25 pm
Posts: 4
Hi,

Have a problem where a duplicate record is being inserted for a one-one association with use of foreign generator. I am calling save, cascade is set to none on th one-ones and the association is to an object fetched in a earlier session.

I have the following object model:

ParentA 1 <------> 0/1 ParentB
| |
1..* 1..*
ChildA 1 <----------> 0/1 ChildB

Which is a bit of a pain, admittedly. ParentA is associated with 0 or 1 ParentBs and has one or more ChildAs. The associations between As and Bs need to be bidirectional.

The table structure is also as above, with a parentAId and a childAId. The B tables share the same key (parentAId or childAId respectively), as current hibernate requires.

The mapping documents are as follows, trimmed out data columns for readability:

hibernate-mapping>
<class name="ParentA" table="ParentA">
<id name="parentAid" column="parentAid" type="long">
<generator class="sequence">
<param name="sequence">s_parentA</param>
</generator>
</id>
<set name="childrenA" cascade="all" inverse="true" sort="natural">
<key column="parentAid"/>
<one-to-many class="ChildA"/>
</set>
</class>
</hibernate-mapping>

<hibernate-mapping>
<class name="ChildA" table="ChildA">
<id name="childAId" type="long">
<column name="childAid" length="19"/>
<generator class="sequence">
<param name="sequence">s_childA</param>
</generator>
</id>
<many-to-one name="parentA" class="ParentA" column="parentAid" cascade="none"/>
</class>
</hibernate-mapping>

<hibernate-mapping>
<class name="ParentB" table="ParentB">
<id name="parentAid" type="long" column="parentAid">
<generator class="foreign">
<param name="property">parentA</param>
</generator>
</id>
<one-to-one name="parentA" class="ParentA" cascade="none"/>
<set name="childrenB" cascade="all" inverse="true" sort="natural">
<key column="parentAid"/>
<one-to-many class="ChildB"/>
</set>
</class>
</hibernate-mapping>


<hibernate-mapping>
<class name="ChildB" table="ChildB">
<id name="childAid" column="childAid" type="long">
<generator class="foreign">
<param name="property">childA</param>
</generator>
</id>
<one-to-one name="childA" class="ChildA" cascade="none"/>
<many-to-one name="parentB" class="ParentB" column="parentAid" cascade="none"/>
</class>
</hibernate-mapping>

The code does the following - with each access to hibernate in a separate session:

1) Get ParentA with id 100 from hibernate, with its ChildAs 1001 and 1002
2) Create a new ParentB and ChildB for each ChildA.
3) Tie the Bs to the As (associations are set at both ends).
4) Call session.update on ParentA (works - updates 100 and children 1001 and 1002).
5) Call session.save on ParentB

upon saving parentB, I end up with two new ChildBs, each one tied to the correct ChildA and correct ParentB (so far so good), but I end up with two ParentAs - the original with id 100 and a new one with ID 101.

There are two things about this I don't understand:

first, why is the save on ParentB cascading to the A tree at all, since I have specified cascase = none.

second, why is the save of the ChildBs, when Parent B cascades, not also causing duplicate ChildAs? It seems like if i get an unwanted ParentA created I'd also be getting unwanted ChildAs.

Driving me crazy.

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Use of foreign generator with one-one associations
PostPosted: Mon Nov 17, 2003 7:22 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
prosper2000 wrote:
The table structure is also as above, with a parentAId and a childAId. The B tables share the same key (parentAId or childAId respectively), as current hibernate requires.

Same value is required, not same name.

prosper2000 wrote:
The code does the following - with each access to hibernate in a separate session:

?? what do you mean by each access in a separate session. Show the exact code, it's easier to understand than you five points.

prosper2000 wrote:
second, why is the save of the ChildBs, when Parent B cascades, not also causing duplicate ChildAs?

Probably related to save != saveOrUpdate because of unsaved-value

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 17, 2003 8:09 pm 
Newbie

Joined: Fri Nov 14, 2003 9:25 pm
Posts: 4
I understand the same value is required for the association to work, but isn't that exactly what the foreign generator is supposed to do? And again, why is it cascading to the A tree at all when a B is saved, when cascase="none" on the associations?

I have punted for the time being - have to make some progress. I'm rolling the tables up (so childB columns move into childA table, same for parent), which will cause some null values, but is easier to understand and much easier to handle in hibernate.

When I have time, I will try to write a unit test to demonstrate the problem I was having here and will post.

thanks


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.