简体   繁体   中英

Hibernate does not delete entries from removed objects in collection

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM