Hibernate JPA OneToOne orphan removal still not working as of 4.2.7/4.3.0.CR1

白昼怎懂夜的黑 提交于 2019-12-20 02:29:57

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/20280271/hibernate-jpa-onetoone-orphan-removal-still-not-working-as-of-4-2-7-4-3-0-cr1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!