I have a many to many relationship between a User and Group object. I have mapped and coded it as shown below, but I when I try to add a user to group it fail - actually nothing happen. No error is thrown and nothing is written to the database.
Any thoughts on where the problem may be?
thanks
Scott
Maps:
Group Map
Code:
<hibernate-mapping>
<class name="Group" table="GRP" >
<comment></comment>
<id name="grpId" type="string">
<column name="GRP_ID" length="20" />
<generator class="assigned" />
</id>
<property name="grpName" type="string">
<column name="GRP_NAME" length="40">
<comment></comment>
</column>
</property>
<set name="users" inverse="true" table="USER_GRP" lazy="true" cascade="all-delete-orphan">
<key>
<column name="GRP_ID" length="20" not-null="true">
<comment></comment>
</column>
</key>
<many-to-many entity-name="User">
<column name="USER_ID" length="20" not-null="true">
<comment></comment>
</column>
</many-to-many>
</set>
</class>
</hibernate-mapping>
User Map:
Code:
<hibernate-mapping>
<class name="User" table="USERS" >
<comment></comment>
<id name="userId" type="string">
<column name="USER_ID" length="20" />
<generator class="assigned" />
</id>
<property name="firstName" type="string">
<column name="FIRST_NAME" length="40">
<comment></comment>
</column>
</property>
<property name="lastName" type="string">
<column name="LAST_NAME" length="40">
<comment></comment>
</column>
</property>
<set name="groups" table="USER_GRP" lazy="true">
<key>
<column name="USER_ID" length="20" not-null="true">
<comment></comment>
</column>
</key>
<many-to-many entity-name="Group">
<column name="GRP_ID" length="20" not-null="true">
<comment></comment>
</column>
</many-to-many>
</set>
</class>
</hibernate-mapping>
Java code in DAO: Code:
public void addUserToGroup(Group group, User user) {
log.debug("adding user to group");
Set<User> userSet = group.getUsers();
userSet.add(user);
group.setUsers(userSet);
try {
sessionFactory.getCurrentSession().saveOrUpdate(group);
log.debug("persist user to group successful");
} catch (RuntimeException re) {
log.error("persist failed", re);
throw re;
}
}