I have a somewhat strange question, I don't know if this is supported in JPA:
I have an @Entity Child
and two other entities, @Entity Parent1
and @Entity Parent2
.
What I would like to do, is have a @OneToMany
relationship between Parent1 and Child, and another @OneToMany
relationship between Parent2 and Child.
The reason is that I want Childs to be deleted if their Parent1 is deleted, and Childs to be deleted if their Parent2 is deleted.
I have tried many combinations but I can't get it to work...
TL;DR: Any Child that does not have a Parent1 AND a Parent2 should be deleted.
Here is my code right now (ommitting @Id and getter/setters):
@Entity
class Parent1 {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
Set<Child> childs;
}
@Entity
class Parent2 {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
Set<Child> childs;
}
@Entity
class Child {
String name;
}
Thank you,