Using Hibernate, I have the following classes :
public class Person {
@ManyToMany(fetch=FetchType.LAZY)
@Cascade(CascadeType.ALL)
@JoinTable(name = "person_address", joinColumns = { @JoinColumn(name = "person_id") },
inverseJoinColumns = { @JoinColumn(name = "address_id") })
public List<Address> getAddresses() {
return addresses;
}
}
public class Address {
...
@ManyToMany(mappedBy="addresses", fetch=FetchType.LAZY开发者_如何学Python)
@Cascade(CascadeType.ALL)
public List<Person> getPersons() {
return persons;
}
}
My question is : Is it possible that deleting an element of the relationship between Address and Person, "orphans" elements of Address are also deleted. In other words I don't want to have addresses that are not linked to a person.
Thanks, Marc.
No, it's not possible. Hibernate doesn't provide orphan removal functionality for many-to-many relationships.
Why would you like to do that? You can delete any of the entities (a Person or an Address) and Hibernate will ensure the consistency based on the annotation you have defined.
Manually deleting links between the different tables is an unnecessary risk in this case.
org.hibernate.annotations.CascadeType.DELETE_ORPHAN
can be used to deleted orphans.
精彩评论