186

I have a problem with a simple @OneToMany mapping between a parent and a child entity. All works well, only that child records are not deleted when I remove them from the collection.

The parent:

@Entity
public class Parent {
    @Id
    @Column(name = "ID")
    private Long id;

    @OneToMany(cascade = {CascadeType.ALL}, mappedBy = "parent")
    private Set<Child> childs = new HashSet<Child>();

 ...
}

The child:

@Entity
public class Child {
    @Id
    @Column(name = "ID")
    private Long id;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="PARENTID", nullable = false)
    private Parent parent;

  ...
}

If I now delete and child from the childs Set, it does not get deleted from the database. I tried nullifying the child.parent reference, but that did not work either.

The entities are used in a web application, the delete happens as part of an Ajax request. I don't have a list of deleted childs when the save button is pressed, so I can't delete them implicitly.

g00glen00b
  • 41,995
  • 13
  • 95
  • 133
bert
  • 7,566
  • 3
  • 31
  • 47

8 Answers8

292

JPA's behaviour is correct (meaning as per the specification): objects aren't deleted simply because you've removed them from a OneToMany collection. There are vendor-specific extensions that do that but native JPA doesn't cater for it.

In part this is because JPA doesn't actually know if it should delete something removed from the collection. In object modeling terms, this is the difference between composition and "aggregation*.

In composition, the child entity has no existence without the parent. A classic example is between House and Room. Delete the House and the Rooms go too.

Aggregation is a looser kind of association and is typified by Course and Student. Delete the Course and the Student still exists (probably in other Courses).

So you need to either use vendor-specific extensions to force this behaviour (if available) or explicitly delete the child AND remove it from the parent's collection.

I'm aware of:

musiKk
  • 14,751
  • 4
  • 55
  • 82
cletus
  • 616,129
  • 168
  • 910
  • 942
  • thanks the nice explanation. so it is as i feared. (i did some search/ reading before i asked, just wanting to be save). somehow i start regretting the decision to use JPA API and nit Hibernate directly .. I will try Chandra Patni pointer and use the hibernate delete_orphan cascade type. – bert Jan 06 '10 at 08:12
  • I have a kind of similar question about this. Would be be kind take a look at this post here, please? http://stackoverflow.com/questions/4569857/jsf-how-to-update-the-list-after-delete-an-item-of-that-list – Thang Pham Jan 01 '11 at 04:33
  • 95
    With JPA 2.0, you can now use the option orphanRemoval = true – itsadok Feb 02 '12 at 13:17
  • 3
    Great initial explanation and good advice on orphanRemoval. Had no idea JPA didn't account for this type of removal. The nuances between what I know about Hibernate and what JPA actually does can be maddening. – sma May 29 '12 at 21:50
  • 1
    Nicely explained by exposing the differences between Composition and Agregation! – Felipe Leão Dec 22 '17 at 19:21
88

In addition to cletus' answer, JPA 2.0, final since december 2010, introduces an orphanRemoval attribute on @OneToMany annotations. For more details see this blog entry.

Note that since the spec is relatively new, not all JPA 1 provider have a final JPA 2 implementation. For example, the Hibernate 3.5.0-Beta-2 release does not yet support this attribute.

mixel
  • 25,177
  • 13
  • 126
  • 165
Louis Jacomet
  • 13,661
  • 2
  • 34
  • 43
60

You can try this:

@OneToOne(orphanRemoval=true) or @OneToMany(orphanRemoval=true).

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
  • 3
    Thanks. But the question is back from the JPA 1 times. And this option was not available back than. – bert Apr 04 '13 at 12:31
  • 17
    good to know nevertheless, for those of us who search for a solution for this in the JPA2 times :) – alex440 Oct 15 '15 at 15:51
23

As explained, it is not possible to do what I want with JPA, so I employed the hibernate.cascade annotation, with this, the relevant code in the Parent class now looks like this:

@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, mappedBy = "parent")
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
            org.hibernate.annotations.CascadeType.DELETE,
            org.hibernate.annotations.CascadeType.MERGE,
            org.hibernate.annotations.CascadeType.PERSIST,
            org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
private Set<Child> childs = new HashSet<Child>();

I could not simple use 'ALL' as this would have deleted the parent as well.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
bert
  • 7,566
  • 3
  • 31
  • 47
7
@Entity 
class Employee {
     @OneToOne(orphanRemoval=true)
     private Address address;
}

See here.

Community
  • 1
  • 1
Kim
  • 980
  • 1
  • 15
  • 29
4

Here cascade, in the context of remove, means that the children are removed if you remove the parent. Not the association. If you are using Hibernate as your JPA provider, you can do it using hibernate specific cascade.

Chandra Patni
  • 17,347
  • 10
  • 55
  • 65
4

You can try this:

@OneToOne(cascade = CascadeType.REFRESH) 

or

@OneToMany(cascade = CascadeType.REFRESH)
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
3

I don't know why, but I had the same issue and my Entities were like below:

public class Parent {
    @OneToMany(mappedBy = "parent",
            cascade = CascadeType.ALL)
    private List<Child> childList= new ArrayList<>();
}

public class Child {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PARENT_ID", nullable = false)
    private Parent parent;
}

And I could finally delete a single child when I wrote my own delete() method:

@Modifying
@Query("delete from Child c where c.id = :id")
void deleteById(Long id);

please consider that using @Modifying is necessary

And I called it from Service layer:

childRepository.deleteById(childInstance.getId());
Sobhan
  • 1,280
  • 1
  • 18
  • 31