Hibernate many-to-many mapping and cascade=delete
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
<clas开发者_StackOverflows 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?
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.
Why not simply call role.getAssignments().clear()
before deleting the role?
for (Person p : role.getAssignments()) {
person.getRoles.remove(role)
}
role.getAssignments.clear();
session.delete(role);
I'm not a big fan of cascade="delete"
. I get this weird gut feeling any time I think about a snippet of XML being able to delete entire tables of valuable data :)
Don't put a mapping on the Role
's side at all. If you eventually need this information, get it with an HQL query. I.e. get rid of this:
<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>
And whenever you need the persons for a role (which I think should be rare) get it by:
SELECT p FROM Person p JOIN p.roles WHERE role=:role
精彩评论