vendredi 17 avril 2015

Make 2 different Parent Entities reference a Child Entity through @OneToMany in JPA

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...


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,


Aucun commentaire:

Enregistrer un commentaire