Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.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 Hibernate集合保留对已删除实体的引用_Java_Hibernate_Orm_Spring Data Jpa - Fatal编程技术网

Java Hibernate集合保留对已删除实体的引用

Java Hibernate集合保留对已删除实体的引用,java,hibernate,orm,spring-data-jpa,Java,Hibernate,Orm,Spring Data Jpa,我们使用SpringDataHibernate作为数据库访问的ORM。我的实体设置如下: class E1 { @Column(name = "e1_id") private BigDecimal e1_id; @OneToMany(cascade = CascadeType.ALL, mappedBy = "e1", orphanRemoval=true, fetch = FetchType.EAGER) @Fetch(value = FetchMode

我们使用SpringDataHibernate作为数据库访问的ORM。我的实体设置如下:

class E1 {
    @Column(name = "e1_id")
    private BigDecimal e1_id;   

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "e1", orphanRemoval=true, fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SUBSELECT)
    private Collection<E2> e2List;
}

class E2 {
    @Column(name = "e2_id")
    private BigDecimal e2_id;

    @JoinColumn(name = "e1_id", referencedColumnName = "e1_id")
    @ManyToOne
    private E1 e1;
}
   detachedE1.setE2Collection(Collections.emptyList())
   e1Repo.save(detachedE1)
e1具有e2的非空集合

我从e1中删除e2实体,然后在spring事务中保存e1

E1 detachedE1 = e1Repo.save(e1);
然后,我尝试使用detachedE1,如下所示:

class E1 {
    @Column(name = "e1_id")
    private BigDecimal e1_id;   

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "e1", orphanRemoval=true, fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SUBSELECT)
    private Collection<E2> e2List;
}

class E2 {
    @Column(name = "e2_id")
    private BigDecimal e2_id;

    @JoinColumn(name = "e1_id", referencedColumnName = "e1_id")
    @ManyToOne
    private E1 e1;
}
   detachedE1.setE2Collection(Collections.emptyList())
   e1Repo.save(detachedE1)
上面的代码出错了,错误基本上是这样的:

springframework.orm.jpa.JpaObjectRetrievalFailureException:找不到id为1.00的entity.E2

我的问题是,JPA如何保留对已删除集合的引用。我将E2集合设置为空列表。我还验证了e1Repo.find(detachedE1.e1_id)返回的实体具有空集合

提前感谢您的回复/指点


只是想添加我们使用的hibernate hibernate-entitymanager-4.3.8.Final.jar

,所以在使用调试器挖掘hibernate代码之后,我终于找到了答案。分离实体的集合实际上是一个PersistentBag,这不应该太令人惊讶。 但是持久性包维护一个快照,当我将集合设置为空列表时,快照不会被清除

为了验证我的最终理论,我添加了以下内容:

((PersistentCollection)e1.getE2Collection()).setSnapshot(null, null, null);
在此之后,hibernate持久上下文能够成功合并分离的实体,E2集合设置为空列表