Hibernate deleting orphans when updating collection
for people searching for their solution: Now in Hibernate, resp. JPA 2.0, this is the right way:
@OneToMany(orphanRemoval=true)
The cascade option in the @OneToMany
annotation is a array, what you want is:
@OneToMany(cascade={CascadeType.ALL, CascadeType.DELETE_ORPHAN})
You're not doing anything wrong. You're just not removing the child entity. You can either do this with an explicit remove() of the child entity (in addition to what you're doing) or use that annotation that causes orphan records to be deleted.
Also, it's worth mentioning that CascadeType.DELETE
won't delete orphans either. That means something else. See JPA CascadeType.ALL does not delete orphans.
Basically to do this automatically you'll want this on the collection in the parent:
org.hibernate.annotations.CascadeType.DELETE_ORPHAN