Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA orphan removal does not work for OneToOne relations

Tags:

Does anyone have a workaround for this issue: https://hibernate.atlassian.net/browse/HHH-9663?

I am also facing a similar issue. When I created one-sided (no reverse reference) one to one relationship between two entities and set the orphan removal attribute to true, the referenced object is still in the database after setting the reference to null.

Here is the sample domain model:

@Entity
public class Parent {
  ...
  @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
  @JoinColumn(name = "child_id")
  private Child child;
  ...
}

@Entity
public class Child {
  ...
  @Lob
   private byte[] data;
  ...
}

I am currently working around this by manually deleting orphans.

like image 588
ankurvsoni Avatar asked Jul 17 '15 07:07

ankurvsoni


People also ask

What is orphan removal in JPA?

The orphanRemoval attribute is going to instruct the JPA provider to trigger a remove entity state transition when a PostComment entity is no longer referenced by its parent Post entity.

Does orphanRemoval need to be set to true in order to support removing elements from a collection?

If orphanRemoval is set to true, the line item entity will be deleted when the line item is removed from the order. The orphanRemoval attribute in @OneToMany and @oneToOne takes a Boolean value and is by default false.

How does hibernate cascade work?

Cascading is a feature in Hibernate, which is used to manage the state of the mapped entity whenever the state of its relationship owner (superclass) affected. When the relationship owner (superclass) is saved/ deleted, then the mapped entity associated with it should also be saved/ deleted automatically.


1 Answers

Cascading only makes sense for entity state transitions that propagate from a Parent to a Child. In your case, the Parent was actually the child of this association (having the FK).

Try with this mapping instead:

@Entity
public class Parent {
  ...
  @OneToOne(
      fetch = FetchType.LAZY, 
      cascade = CascadeType.ALL, 
      orphanRemoval = true, 
      mappedBy = "parent"
  )
  private Child child;
  ...
}

@Entity
public class Child {

    @OneToOne
    @JoinColumn(name = "parent_id")
    private Parent parent;

    ...
    @Lob
    private byte[] data;
    ...
}

And to cascade the orphan removal, you now need to:

Parent parent = ...;
parent.getChild().setParent(null);
parent.setChild(null);

Or even better, confgiure the setChild method in the Parent entity class to set both associations:

public void setChild(Child child) {
    if (child == null) {
        if (this.child != null) {
            this.child.setParent(null);
        }
    }
    else {
        child.setParent(this);
    }
    this.child = child;
}
like image 70
Vlad Mihalcea Avatar answered Sep 19 '22 13:09

Vlad Mihalcea