Section 6.8 of the user manual states that a bidirectional relationship is maintained by one end of the relationship. I have a situation where I really want both sides to manage the relationship.
This has to do with Users and UserGroups: One User can belong to many UserGroups, and a UserGroup can belong to many users. Everything works fine, except when I want to remove a UserGroup. The desired behavior is that when I remove a User, I want User-UserGroup relationship to be removed, and when I remove a UserGroup, I also want the that User-UserGroup relationship removed. The problem is, when I attempt to remove a UserGroup that's assigned to a user, I get an exception and the UserGroup is not removed (neither is the relation to User(s)). Here are my mapping files:
Code:
<hibernate-mapping>
<class name="SuiteUser" table="SuiteUser">
<id name="ID" type="long" column="ID" unsaved-value="null" >
<generator class="native"/>
</id>
<property name="userName" column="userName" type="string"/>
<set name="userGroups" table="group_membership" cascade="all">
<key>
<column name="user_id" not-null="true"/>
</key>
<many-to-many class="UserGroup">
<column name="group_id" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping>
<class name="UserGroup" table="UserGroup">
<id name="id" type="long" column="id" unsaved-value="null" >
<generator class="native"/>
</id>
<property name="description" column="description" type="string"/>
<property name="groupName" column="groupName" type="string" not-null="true" />
<set name="users" table="group_membership" inverse="true" lazy="true">
<key>
<column name="group_id" not-null="true"/>
</key>
<many-to-many class="SuiteUser">
<column name="user_id" not-null="true"/>
</many-to-many>
</set>
</class>
</hibernate-mapping>
Thanks for any suggestions,
Frank