[英]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: 我有一个有点奇怪的问题,我不知道JPA是否支持这个问题:
I have an @Entity Child
and two other entities, @Entity Parent1
and @Entity Parent2
. 我有一个
@Entity Child
和另外两个实体, @Entity Parent1
和@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. 我想做的是,在Parent1和Child之间有
@OneToMany
关系,在Parent2和Child之间有另一个@OneToMany
关系。
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. 原因是如果删除Parent1,我希望删除Childs;如果删除Parent2,则删除Childs。
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. TL; DR :应删除没有Parent1和Parent2的任何Child。
Here is my code right now (ommitting @Id and getter/setters): 这是我的代码(省略@Id和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, 谢谢,
Reading OneToMany.ophanRemoval , you can try this: 阅读OneToMany.ophanRemoval ,你可以试试这个:
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval=true)
Set<Child> childs;
Good luck! 祝好运!
Yes as per @jmvivo answer you need to use orphanRemoval=true is solution of your use-case , Here As per Oracle On this link 是的,根据@jmvivo的回答你需要使用orphanRemoval = true是你的用例的解决方案,这里按照Oracle在这个链接
When a target entity in one-to-one or one-to-many relationship is removed from the relationship, it is often desirable to cascade the remove operation to the target entity.
当从关系中移除一对一或一对多关系中的目标实体时,通常希望将移除操作级联到目标实体。 Such target entities are considered “orphans,” and the orphanRemoval attribute can be used to specify that orphaned entities should be removed.
此类目标实体被视为“孤儿”,orphanRemoval属性可用于指定应删除孤立实体。 For example, if an order has many line items and one of them is removed from the order, the removed line item is considered an orphan.
例如,如果订单包含多个订单项,并且其中一个订单项已从订单中删除,则已删除的订单项会被视为孤儿。 If orphanRemoval is set to true, the line item entity will be deleted when the line item is removed from the order.
如果将orphanRemoval设置为true,则在从订单中删除订单项时,系统商品实体将被删除。
You might also want to look at below SO Question as you go further with your requirement 您可能还希望在进一步了解您的要求时查看下面的SO问题
One to Many relationship JPA/Hibernate removing links 一对多关系JPA / Hibernate删除链接
JPA 2.0 orphanRemoval=true VS on delete Cascade JPA 2.0 orphanRemoval = true删除Cascade时的VS.
You will have to handle this on service/DAO level, I don't think you'll find any JPA support for this specific use case. 您将不得不在服务/ DAO级别上处理此问题,我认为您不会为此特定用例找到任何JPA支持。 Just manually check the conditions prior to deleting the parent/child.
只需在删除父/子之前手动检查条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.