I have a mapping with three entity classes that have a cyclic reference:
Code:
class A {
@OneToMany
@Cascade (value=CascadeType.ALL)
B b;
}
class B {
@OneToOne(optional=false)
C c
}
class C {
@OneToOne(optional=false)
A a;
}
So an A can have many B's which have one C's each. C has a reference to his A.
Now I want to delete an entity of type A with its B's and C's. If I use session.delete(a) the B's get deleted by cascade, but the C's don't. Therefore I have to delete the C's by session.delete(c), but now the cyclic reference is a problem as I cannot delete A (C needs it) and also not C (B needs it). I am getting an PropertyValueException (C.a is referencing a null or transient value).
My solution now is to make the reference from C -> A to optional=true so that I can delete A (with B's by cascade) first and C's at the end.
Is their another solution without making the reference optional (as it is only null within this delete transaction)?
EDIT: Adding a delete cascade from B to C is no possible option in my case.
Thanks in advance,
Olel