[hibernate-users] Many-to-Many attributed mapping issue

Robert La Ferla robert.laferla at o-ms.com
Mon Jan 11 20:32:26 EST 2010


Hello.  I am new to this list but I've scoured the web for a solution 
and cannot find a solution to this issue.

I am using Hibernate 3.x with XML mapping files but under Grails/GORM.  
I have two tables:  Groups and Users with a M-M correlation table called 
GroupsUsers.  There are a few extra attributes on the GroupsUsers table 
like "role".  The problem is that I need to be able to create a new 
Group along with some new GroupsUsers instances and save all of them 
when the Group (newly instantiated or freshly loaded) is saved.  
Conversely, when a User is saved (new or existing), any GroupsUser 
instances need to be saved along with it.  I want Hibernate to do all 
the work.

The problem is that the GroupsUsers instance needs a foreign key to the 
Group but the Group is not yet saved.  I thought the cascade option on 
the groupsUsers association would take care of that but it doesn't.  
Maybe it's a problem with the composite primary key?  I tried modelling 
that with just the id's as "long" values but that didn't work.

Ideas?  Or better yet, this must be a fairly common design pattern so 
sample code would be ideal if you have it...

GroupsUsers.hbm.xml:
===============

<hibernate-mapping>
<class name="com.mycompany.GroupsUsers" table="GROUPSUSERS">
<composite-id class="com.mycompany.GroupsUsersPK" name="groupsUsersPK">
<key-many-to-one name="user" class="com.mycompany.User" column="USER_ID"/>
<key-many-to-one name="group" class="com.mycompany.Group" 
column="GROUP_ID"/>
</composite-id>
<property name="role" column="ROLE" type="string"/>
<property name="active" column="ACTIVE" type="yes_no"/>

<many-to-one name="group" class="com.mycompany.Group" column="GROUP_ID" 
not-null="true" insert="false" update="false"/>
<many-to-one name="user" class="com.mycompany.User" column="USER_ID" 
not-null="true" insert="false" update="false"/>
</class>
</hibernate-mapping>

Groups.hbm.xml:
============

<hibernate-mapping>

<class name="com.mycompany.Group" table="GROUPS">
<id name="id" column="ID" type="long">
<meta attribute="use-in-tostring">true</meta>
<generator class="seqhilo">
<param name="sequence">keyseedsequence</param>
<param name="max_lo">999</param>
</generator>
</id>

<timestamp name="updated" column="UPDATED" />

<property name="name" column="NAME" type="string"><meta 
attribute="use-in-tostring">true</meta></property>
<property name="description" column="DESCRIPTION" type="string"/>

<set name="groupsUsers" table="GROUPSUSERS" cascade="all,delete-orphan" 
inverse="true">
<key column="GROUP_ID"/>
<one-to-many class="com.mycompany.GroupsUsers"/>
</set>
</class>

</hibernate-mapping>

The Users.hbm.xml mapping is similar to Groups.hbm.xml


Example save:

Group group = new Group();
group.setName("Test");
group.setDescription("Just testing...");

User user = User.findById(123456);

GroupsUsers gu = new GroupsUsers();
gu.setRole("Principal");
gu.setGroup(group);
gu.setUser(user);

GroupsUsersPK pk = new GroupsUsersPK(); // composite primary key
pk.setGroup(group);
pk.setUser(user);
gu.setGroupsUsersPK(pk);

group.getGroupsUsers().add(gu);
group.save(flush:true); // GORM save and flush






More information about the hibernate-users mailing list