Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring数据JPA-显式删除子项后加载父项将返回已删除子项的子项集合_Java_Spring_Spring Data_Spring Data Jpa - Fatal编程技术网

Java Spring数据JPA-显式删除子项后加载父项将返回已删除子项的子项集合

Java Spring数据JPA-显式删除子项后加载父项将返回已删除子项的子项集合,java,spring,spring-data,spring-data-jpa,Java,Spring,Spring Data,Spring Data Jpa,我有如下父->子双向关系 class Parent{ @OneToMany(mappedBy="parent", fetch = FetchType.EAGER) Collection<Child> children; } class Child{ @ManyToOne @JoinColumn(name="PARENT_ID") private Parent parent; } 类父类{ @OneToMany(mappedBy=“pare

我有如下父->子双向关系

class Parent{

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

class Child{
    @ManyToOne
    @JoinColumn(name="PARENT_ID")
    private Parent parent;
}
类父类{
@OneToMany(mappedBy=“parent”,fetch=FetchType.EAGER)
收集儿童;
}
班童{
@许多酮
@JoinColumn(name=“PARENT\u ID”)
私人家长;
}
当我显式删除子对象,然后加载它的父对象(和所有子对象)时,我会在父对象的子对象集合中得到以前删除的子对象。。。JPA提供程序是Hibernate

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(); 
Child-Child=childRepo.findOne(Child\u ID);
childRepo.删除(child);
childRepo.flush();
//接下来,返回不包含已删除子项的集合
集合子对象=childRepo.findAll();
Parent Parent=parentRepo.findById(Parent\u ID);
///接下来,返回包含已删除子项的集合
集合parentChildren=parent.getChildren();
我不明白是什么问题?每个find*方法都执行select(在列表中,这些选择都记录在控制台中),但它们返回不同的结果…

您的manytone是渴望的(默认情况下)。你的前配偶也很渴望(你明确地标记了它)。因此,当您在第一行代码中得到一个子代码时,JPA还将加载其父代码以及父代码的所有子代码


然后删除子项,但不将其从父项的子项中删除。由于父级的子级集合已加载,因此已删除的子级仍在集合中。

非常感谢您。。。我知道OneToMany的默认FetchType是懒惰的,我也认为许多OneToMany的默认FetchType也是懒惰的。。。