Reputation: 1360
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)
@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.
Upvotes: 2
Views: 4569
Reputation: 386
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.
Upvotes: 1
Reputation: 242686
No, it's not possible. Hibernate doesn't provide orphan removal functionality for many-to-many relationships.
Upvotes: 3
Reputation: 3084
org.hibernate.annotations.CascadeType.DELETE_ORPHAN
can be used to deleted orphans.
Upvotes: 0