-->
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.  [ 6 posts ] 
Author Message
 Post subject: Problem with multiple one-to-many association and cascade
PostPosted: Thu Nov 27, 2003 6:07 am 
Newbie

Joined: Thu Nov 13, 2003 12:03 pm
Posts: 18
Location: France
Hi,
I have this porblem :
I have a class A with two different associations towards a class B
(ie, one with role1, and one with role2)
Then I try to save A, and I want to save or update (or not ) B.
So I defined two one-to-many relationship with the option cascade="save-update".
But when it happens that the same instance of B is associated with the two different roles to the instance of A I want to save I get a :
"Another object was associated with this id (the object with the given id was already loaded): " because it first does a create and then wants to update the second one.....
Moreover, I don't want to know if the B-end association is the same one on the two roles.....

How can I avoid this problem ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 6:15 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
But when it happens that the same instance of B is associated with the two different roles


No, you are mistaken. You have two instances, both with the same id.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 6:19 am 
Newbie

Joined: Thu Nov 13, 2003 12:03 pm
Posts: 18
Location: France
It is because I am in a disconnected mode.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 7:16 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
If I understand you well
You Load an object graph (A -> Bs where Bs contains B)
You close your session
You add a B snapshot (ie same id, version) from the outside world into A -> Bs2
You open a session
You save A and it fails because B with this id is already in the graph.

I don't know how to solve this in a layered architecture where the top layer don't know of hibernate.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 7:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The solution could involve lock()ing existing Bs before retrieving A, so that you preserve identity


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 9:44 am 
Newbie

Joined: Thu Nov 13, 2003 12:03 pm
Posts: 18
Location: France
The only workaround I found was to check progammaticaly before saving if the instances are identical. And then associate the two roles with the same instance.
If there is any better idea....


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