简体   繁体   English

Spring Data JPA-在显式删除子级后加载父级将返回具有已删除子级的子级的集合

[英]Spring Data JPA - loading parent after explicit child deletion returns collection of children with deleted child

I have Parent->Child bidirectional relationship as follows... 我有如下的父/子双向关系...

class Parent{

    @OneToMany(mappedBy="parent", fetch = FetchType.EAGER)
    Collection<Child> children;
}

class Child{
    @ManyToOne
    @JoinColumn(name="PARENT_ID")
    private Parent parent;
}

When i delete child explicitly, and after that load its parent (with all children) i get previously deleted child in children collection of parent... JPA provider is Hibernate... 当我显式删除子级后,在加载其父级(包括所有子级)后,我在父级的子级集合中得到了先前删除的子级。JPA提供程序是休眠的。

Child child= childRepo.findOne(CHILD_ID);

childRepo.delete(child);
childRepo.flush();

// next, returns collection without deleted child
Collection<Child> children= childRepo.findAll(); 

Parent parent = parentRepo.findById(PARENT_ID);

/// next, returns collection including deleted child
Collection<Child> parentChildren = parent.getChildren(); 

I don't understand what is the problem? 我不明白是什么问题? Every find* method executes select (at list, those SELECTs are logged in console) , but they returns different results... 每个find *方法都执行select(在列表中,这些SELECT被记录在控制台中),但是它们返回不同的结果...

Your ManyToOne is EAGER (by default). 您的ManyToOne是EAGER(默认情况下)。 Your OneToMany is also EAGER (you explicitely marked it so). 您的OneToMany也是EAGER(您已明确标记为)。 So, when you get a child in your first line of code, JPA also loads its parent, and all the children of the parent. 因此,当您在代码的第一行中生了一个孩子时,JPA还将加载其父对象以及该父对象的所有孩子。

Then you delete the child, but you don't remove it from the parent's chidren. 然后,您删除了孩子,但没有将其从父母的孩子中删除。 And since the parent's collection of children is already loaded, the deleted child is still in the collection. 并且由于已经加载了父级的子级集合,因此删除的子级仍在集合中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JPA继承-父项不会与子项一起删除 - JPA Inheritance - parent is not deleted with child 软删除:在Spring Boot JPA Hibernate中删除@OneToMany关系中的父实体后,不会删除子实体 - Soft Delete : Child Entity not being deleted after Deleting parent Entity in @OneToMany relation in Spring Boot JPA Hibernate 在Spring Data JPA中获取具有子级筛选集合的父级实体列表 - Fetching a list of parent entities with a filtered collection of children in Spring Data JPA 如何使用 Lombok 在 Spring/JPA/Hibernate 中获取带有所有子实体和子实体的父实体 - How to get parent entity with all child entities and child entities of children in Spring/JPA/Hibernate with Lombok 春天在OneToMany JPA Relationship中使用子数据获取父数据 - Fetching parent data using Child Data in OneToMany JPA Relationship in spring Spring Data JPA。 如何仅按子集合属性过滤子集合结果? - Spring Data JPA. How to filter by child collection attribute child collection result only? Spring Data Jpa OneToMany 同时保存子实体和父实体? - Spring Data Jpa OneToMany save child and parent entities at the same time? 从子级到父级的Spring-Data-Jpa级联 - Spring-Data-Jpa Cascading from child to parent "Spring Data JPA 在不获取父级的情况下保存子级" - Spring Data JPA save child without fetch the parent @OneToMany 和 @ManyToOne Spring 数据 JPA 子不更新父 - @OneToMany and @ManyToOne Spring Data JPA Child does not update Parent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM