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.
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.