从4.2.7 / 4.3.0.CR1起,休眠JPA OneToOne孤儿删除仍然不起作用 [英] Hibernate JPA OneToOne orphan removal still not working as of 4.2.7/4.3.0.CR1

查看:136
本文介绍了从4.2.7 / 4.3.0.CR1起,休眠JPA OneToOne孤儿删除仍然不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读
JPA 2.0 / Hibernate和orphanRemoval:只是替换一个实体不会删除旧的,并且相关的故障单 https://hibernate.atlassian.net/browse/HHH-6484 ,我推断这已经(最终)在版本4.2.7和4.3.0中得到修复。 CR1。



然而,尝试

  ... 
entityManager.getTransaction()。begin();
Point point = entityManager.find(Point.class,pointId);
point.setPost(null);
entityManager.getTransaction()。commit();
...

其中

  public class Point {
...
@OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY,orphanRemoval = true)
private帖子;
...
public void setPost(Post post){
this.post = post;


$ / code>

仍然不会让Hibernate发出 DELETE 针对目标 Post 实体的SQL语句。 code> @OneToOne(cascade = CascadeType.ALL,orphanRemoval = true)问题是否已修复?如果是这样,我怎样才能让孤儿被删除?非常感谢!

编辑:
在阅读您的答案后,我注意到我(错误地)忽略了指定 fetch = FetchType .LAZY 在我上面的映射中。

解决方案

好吧,您提到的问题涉及级联在将关系设置为新实例时删除。你设置为null,所以它不是真的一样。



我在Hibernate 4.1.8下测试了你的代码(即设置Post为null)邮政条目被删除。我测试了将Post设置为现有Point的新实例,并且删除 not 触发,这与您引用的问题一致。



然后我在4.2.7下进行了测试,并删除了这两种情况,所以你引用的问题确实在4.2.7中得到修复。

更新:

我不明白为什么Fetch提示会影响持久性操作,但我尝试过它已经到位,结果和以前一样。

Having read JPA 2.0 / Hibernate and "orphanRemoval": Just replacing an entity does not remove the old one, and the related ticket https://hibernate.atlassian.net/browse/HHH-6484, I inferred that this had been (finally) fixed in version 4.2.7 and 4.3.0.CR1.

However, trying

...
entityManager.getTransaction().begin();
Point point = entityManager.find(Point.class, pointId);
point.setPost(null);
entityManager.getTransaction().commit();
...

where

public class Point {
    ...
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
    private Post post;
    ...
    public void setPost(Post post) {
        this.post = post;
    }
}

still does not make Hibernate issue a DELETE SQL statement for the target Post entity.

So has this @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true) issue been fixed or not? and if so, how do I get the orphans to be deleted? Many thanks!

EDIT: After reading your answer, I noted that I (mistakenly) omitted to specify that the fetch=FetchType.LAZY in my mapping above.

解决方案

Okay, the issue you refer to relates to a cascading delete when you have set the relationship to a new instance. You are setting to null so it is not really the same.

I tested your code (i.e. setting Post to null) under Hibernate 4.1.8 and it works as expected with the Post entry being deleted. I tested setting the Post to a new instance for an existing Point and the delete was not triggered which is consistent with the issue you refer to.

I then tested under 4.2.7 and the delete was fired for both cases so the issue you refer to is indeed fixed in 4.2.7.

Update:

I don't see why the Fetch hint would affect a persist operation however I have tried with it in place and the results were as before.

这篇关于从4.2.7 / 4.3.0.CR1起,休眠JPA OneToOne孤儿删除仍然不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆