Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top