I have a users and groups. A user can be in 0 to many groups and a group can have 0 to many users. Each association between group and user has an attribute (administrator).
I have tried using a composite-element as described here
http://hibernate.org/118.html#A11
Where I created a UserGroupAssocation class that looked as follows
class UserGroupAssocation {
User user;
Group group;
boolean administrator;
}
class User {
Long uid;
String name;
Set groupAssociations;
}
class Group {
Long uid;
String name;
Set userAssociations;
}
create table user_groups (
group_uid BIGINT not null,
user_uid BIGINT not null,
administrator BIT
)
The problem is when hibernate loads the UserAssociations for a Group the group field is null and for a User the user field in the UserAssocaitons is null. Is there a simple fix to this?
Is there a better way to handle this mapping? Ultimately I need what I said in the first sentence? i.e. to be able to get the groups a user is in and know if that user is an administrator and for a group get the users in that group and know who the groups administrators are.
Here is the pertinent part of the mapping
Group.hbm.xml
<set name="userAssociations" table="user_groups" lazy="true" access='field'>
<key column="group_uid"/>
<composite-element class="UserGroupAssociation">
<many-to-one name='user' class='User' column='user_uid' not-null="true" access='field'/>
<property name='administrator' type='boolean' column='administrator' access='field'/>
</composite-element>
</set>
User.hbm.xml
<set name="groupAssociations" table="user_groups" lazy="true" access='field'>
<key column="user_uid"/>
<composite-element class="UserGroupAssociation">
<many-to-one name='group' class='Group' column='group_uid' not-null="true" access='field'/>
<property name='administrator' type='boolean' column='administrator' access='field'/>
</composite-element>
</set>