Java – Hibernate many-to-many mapping and cascade=delete

hibernatejavaorm

I have a mapping (only important parts):

<class name="xyz.Role" table="ROLE" lazy="true">
  <id name="id" type="java.lang.Integer">
    <column name="ROLE_ID"/>
    <generator class="increment"/>
  </id>
  <set name="assignments" lazy="true" table="PERSON_ROLE" cascade="delete" 
inverse="true">
    <key column="ROLE_ID" />
    <many-to-many class="xyz.Person" column="PERSON_ID" />
  </set> 
</class>

and

<class name="xyz.Person" table="PERSON" lazy="true">
  <id name="Id" type="java.lang.Integer">
    <column name="TPP_ID"/>
    <generator class="increment"/>
  </id>

  <set name="roles" lazy="true" table="PERSON_ROLE" cascade="save-update">
    <key column="PERSON_ID" />
    <many-to-many class="xyz.Role" column="ROLE_ID" />
  </set> 
</class>

With this mapping, when I delete a role, very person with this role is also deleted. What I would like to achieve, is delete the association (row from PERSON_ROLE table) when I delete Role. Is there any way to achieve this?

Best Answer

Cascade works on the level of entities. Since Person_Role is not mapped as entity, cascade can not help you AFAIK.

You might use a database-level "on cascade delete" on the foreign key from Person_Role to Role.

Or you can - as sfussenegger points out - remove the association programatically. Note that since you mapped the association on both entities, every row in Person_Role will appear twice in your object model. In such cases, it is recommended to remove the relevant entries from both collections in order not to corrupt your object model. Hibernate however will only look at the end of the association that is not mapped with inverse="true" when persisting changes. That is, to delete from the association with your current mapping, you must delete from Person.roles, not Role.assignments:

for (Person p : role.assignments) {
    person.roles.remove(role)
}

Or you might wish to replace the many-to-many mapping with an association entity, in which case you could simply use cascade. This would allow you to add more information to assignments more easily. For instance, if you had to express "Joe works 30% on QA and 70% as requirements engineer", you could simply add that field to the association.