-->
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: a different object with the same identifier value
PostPosted: Tue Jan 20, 2009 7:13 pm 
Newbie

Joined: Wed Jul 30, 2008 1:03 am
Posts: 16
Hello,

I have the following entities:


Code:
class A {
  ...
}

class B {
  A a; // setter and getter
  Set<D> item; // setter and getter
  ...
}

class C {
  A a; // setter and getter
  Set<D> item; // setter and getter
  ...
}

class D {
  ...
}


I have already instances of A, B and C in the db. Now I want to create a brand new D:

Code:
session = ...
B b = session.get(B.class, bId);
C c = session.get(C.class, cId);
// both b and c have the same reference to an A instance

D d = new D();
b.setItem(d);
c.setItem(d);
session.saveOrUpdate(d);



Now I'm getting "a different object with the same identifier value". It is referring to A instance of b and the A instance of c. They must point to the same object but they are not.

I read that one needs to merge(). But in my case why do I have to merge or even better what do i have to merge? Is it not the responsibility of Hibernate to know that the A instance of B and C are the same object and merges them automatically/transparently in the persistence context? Or am i doing this wrong?

Appreciate your help.

Ramin


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 25, 2009 7:38 pm 
Newbie

Joined: Wed Jul 30, 2008 1:03 am
Posts: 16
Any help, please?

What I did to bypass this problem is to set the fetch startegy for A in both B and C to lazy. Now I don't have the "a different object with the same identifier value" error. However, since the A property in B and C is lazy, when I'm in detached mode, i seem not to be able to initialize() it since I'm using spring @Transactional and its support for the session management. The only thing I could find on many posts is to use OSIV. But in my case my junit test is failing and since it's being considered a console appl. i cannot apply OSIV.

I know I have two completely different issues but any pointer for anyone of them is greatly appreciated.

Ramin


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2009 12:41 am 
Regular
Regular

Joined: Wed Apr 25, 2007 11:44 pm
Posts: 59
try this one
Code:
session = ...
B b = session.get(B.class, bId);
C c = session.get(C.class, cId);
// both b and c have the same reference to an A instance

D d = new D();
[i]
b.getItem().clearAll();
c.getItem().clearAll();
[/i]
b.setItem(d);
c.setItem(d);
session.saveOrUpdate(d);


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.