Hibernate does not delete entries from removed objects in collection

Viewed 68

I have two collections, groups and permissions. The first collection works well and removes the entries which got removed from the collection. But the permissions collection does not do what it should. I always remove entries from the collection but the changes won't be updated in the DB.

    @ManyToMany
    @JoinTable(name = "user_groups")
    private List<Group> groups = new ArrayList<Group>();

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    @OrderBy("name ASC")
    private List<UserPermission> permissions = new ArrayList<UserPermission>();

The same code is used to save the Entity containing these two lists and equal code to remove the entries.

1 Answers

Try to use the orphanRemoval = true the orphan-removal allows you to remove the Child entity whenever it’s no longer referenced by its Parent:

@OneToMany(cascade = CascadeType.ALL, mappedBy = "user", orphanRemoval = true)
    @OrderBy("name ASC")
    private List<UserPermission> permissions;
Related