-->
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: Can Hibernate handle duplicated objects?
PostPosted: Mon Aug 04, 2008 7:34 am 
Newbie

Joined: Fri Aug 01, 2008 2:20 pm
Posts: 6
Hi all,

I encountered a problem with NHibernate.

I have a project that has 2 sub-contractors.
First sub-contractor S1 has 2 members A and B.
Second sub-contractor S2 has 2 members A and C.

So when saving that project I receive an error “a different object with the same identifier value was already associated with the session”. That object is A.

Obviously S1 and S2 have duplicated users A in a session.

Is there a walkaround to it?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 04, 2008 7:51 am 
Newbie

Joined: Thu Jul 31, 2008 7:04 am
Posts: 4
Hi,

you can try with having ManytoMany relationship with User and Sub-Contractor.
Relationship is like 1 user can have multiple sub-Contractor, and one sub-contractor can have multiple user.

The relationship will create a 3rd table and normalized in 3NF. In that table you will have data like:

sub_contractor_id user_id
id of S1 id of A
id of S1 id of B
id of S2 id of A
id of S2 id of C

I guess, whatever i can understand about your problem, this solution will solve the problem.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 04, 2008 8:01 am 
Newbie

Joined: Fri Aug 01, 2008 2:20 pm
Posts: 6
Thanks for your answer. But it does not solve problem.

I do have 3rd table like you said.

The problem here is that Hibernate doesn’t handle this situation because it’s needed to get an object just after been stored (to get the proxy to the object)

When getting object (saved project) the error will occur because there is duplicated member (member A) in a session.

If I remove member A from S1 or S2 then project is saved successfully.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 04, 2008 2:12 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Yes, Hibernate can handle this situation with no problem. The a different object with the same identifier value was already associated with the session error message is usually the result of trying to mix objects that have been loaded by different Hibernate sessions into another Hibernate session. If I find myself in a situation like this, I usually try to re-load the object with the current session.

Eg. something like this:

Code:
MyEntity e = ... // loaded in another session
e = (MyEntity)session.get(e.getClass(), e.getId());


Top
 Profile  
 
 Post subject: Problem solved
PostPosted: Mon Aug 04, 2008 2:27 pm 
Newbie

Joined: Fri Aug 01, 2008 2:20 pm
Posts: 6
Yes, I want to mix asp.net membership objects with NHibernate.

Your solution does solve problem.

Thank you very much!


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.