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