Hibernate Removal Request Order

Here is my data model (simplified),

public class AddressBook { private List<Group> groups = new ArrayList<Group>(); private List<People> peoples = new ArrayList<People>(); @OneToMany(mappedBy = "addressbook", cascade = CascadeType.ALL, fetch = FetchType.LAZY) @OnDelete(action = OnDeleteAction.CASCADE) @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN) public List<Group> getGroups() { return groups; } @OneToMany(mappedBy = "addressbook", cascade = CascadeType.ALL, fetch = FetchType.LAZY) @OnDelete(action = OnDeleteAction.CASCADE) @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN) public List<People> getPeoples() { return peoples; } } public class Group { private AddressBook addressBook; @ManyToOne(fetch = FetchType.LAZY, optional = false) public void setAddressBook(AddressBook addressBook) { this.addressBook = addressBook; } } public class People { private AddressBook addressBook; private Group group; @ManyToOne(fetch = FetchType.LAZY, optional = false) public AddressBook getAddressBook() { return addressBook; } public Group getGroup() { return group; } } 

I want to remove the entire group from my address book and all the people belonging to this group. So I am doing something like:

 adressBook.getPeople().removeAll(peopleBelongingToGroupA); adressBook.getGroups().remove(groupA); 

But when my transaction is complete, Hibernate does first:

 delete from groups where groupName='groupA'; 

Instead of removing people first. This violates the FOREIGN_KEY constraint between people and the group.

Is there a way to tell hibernate to remove people first and then groups? Is there a flaw in my model?

+4
source share
3 answers

You tried to set the cascade setting to each @ManyToOne. You have indicated in many respects cascading deletion in the address book. This property is for every association that I consider.

The EJB3.0 specification deserves attention when writing these beans. See http://jcp.org/en/jsr/detail?id=220

Update: If you read your database again, there may not be an annotation for people who would explain the behavior. Do you have a cascade set by link with people → group? This explains why the first statement first starts by trying to delete the group. Presumably you need annotation for groups of people who don't cascade?

+2
source

there are 2 options

1) clear the check box before the second deletion

2) add a "cascade" to your mappings: i.e. cascade = "delete". Removing a group will delete and delete the members of the group.

+1
source

You can do this as two transactions or use cascade = delete, as mentioned above. In fact, you probably want to remove the orphans, which makes a cascading deletion, but only the Person deletes if they are completely lost. This does 2 nice things:

1) All you do is delete the group, and it automatically removes the children (provided that your mappings are configured correctly). 2) If a Person can belong to more than one group, it will only delete this child of Person, if it remains an orphan, which means that the group does not refer to it.

This second part is a big deal if the child (Person in this case) can have multiple parents, and therefore, the child should not be deleted unless each parent relationship is deleted.

0
source

Source: https://habr.com/ru/post/1285754/


All Articles