简体   繁体   English

使两个不同的父实体通过JPA中的@OneToMany引用子实体

[英]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.

相关问题 JPA @OneToMany -&gt; 父 - 子参考(外键) - JPA @OneToMany -> Parent - Child Reference (Foreign Key) 在JPA双向@OnetoMany关系中,当我更新父实体时,数据库中的子实体被删除 - In JPA bidirectional @OnetoMany relationship, when I update the parent entity, the child entities are deleted in database Spring Data Jpa OneToMany 同时保存子实体和父实体? - Spring Data Jpa OneToMany save child and parent entities at the same time? OneToMany:更新父级后,子级中对父级实体的引用不是最新的 - OneToMany: reference to parent entity is not up to date in child after parent was updated JPA:使用单向@OneToMany时,子实体中的引用列为null - JPA: Reference column in the child entity is null when using unidirectional @OneToMany 在OneToMany JPA中更新父项时,将创建新的子实体 - New child entity is getting created while Updating Parent in OneToMany JPA Spring 数据 JPA:删除 OneToMany 子节点而不经过父节点 - Spring Data JPA: delete OneToMany child without going through the parent 通过JPA联接从父实体返回子实体 - Returning child entities from a parent entity with JPA join 软删除:在Spring Boot JPA Hibernate中删除@OneToMany关系中的父实体后,不会删除子实体 - Soft Delete : Child Entity not being deleted after Deleting parent Entity in @OneToMany relation in Spring Boot JPA Hibernate 如何使用 Lombok 在 Spring/JPA/Hibernate 中获取带有所有子实体和子实体的父实体 - How to get parent entity with all child entities and child entities of children in Spring/JPA/Hibernate with Lombok
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM