I'm developing a webapp with Hibernate 2.1.7 + Spring, using OpenSessionInView.
We have this situation: object A has references to object B and C.
Both objects B and C have references to an object X. And B has a Set of C objects..
All hashCode and equals methods necessary are write. The mapping files are ok, only using common many-to-one
and inverse sets..
Code:
class A {
B b;
C c;
X x;
// ...
}
class B {
X x;
Set cees;
// ...
}
class C {
X x;
B b;
// ...
}
In one case, X has the same entity identifier in the session for A, B and C (but not the same instances!!).
When we go to saveOrUpdate new instance of A, the Hibernate throws NonUniqueObjectException. To avoid
NonUniqueObjectException, I'm trying this, but them the NonUniqueObjectException occurs with C.b id!!!
Code:
class A_DAO {
public void saveA(A a) {
// First:
if (a.b.x.equals(a.c.x)) {
a.b.x = a.c.x; // same entity id and same instance reference...so it's ok to save..
}
// Second!!!
if (a.x.equals(a.b.x)) {
a.x = a.b.x; // same entity id and same instance reference...so it's ok to save..
}
// Last, but here LazyLoadException
if (a.b.equals(a.c.b)) {
a.c.b = a.b;
}
getHibernateTemplate().saveorUpdate(a);
}
}
What's the correct approach for this situation? It's wrong(?) and ugly to write lot's of code in the DAO's for this
cases and my model has "n" situations where multiple references to the same entity id could occurs..
Thanks in advance