Hello everybody
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 delete a full group from my addressbook, and all the people belonging to this group. So I do code somthing like:
adressBook.getPeople().removeAll(peopleBelongingToGroupA);
adressBook.getGroups().remove(groupA);
But when my transaction is commited, Hibernate does first:
delete from groups where groupName='groupA';
instead of deleting the people first. That causes my FOREIGN_KEY constraint between people and group to be violated...
Is there a way to tell hibernate to delete the people first, then the groups? Is there a flaw in my model?
Thank you for your help!