Hibernate version:
2.1.6
Hi All,
I am struggling with the ternary association. Here is my model:
* a department has many Programs
* each program is associated with a set of Roles. for example:
Program Manager, Program Member, etc.
each program can have a different set of Roles.
* User can participate in multiple Programs with a set of different Roles
So I think we need three entities:
Program, Role and User.
The relationships are:
Program <many-to-many> Role
User <many-to-many> Program
and there is a ternary association: User with Roles per Program.
I think the POJO classes will look like these:
---------------------------------------------------
Class Program {
......
Set roles = new HashSet (); <--- a set of Role
Set userRoles = new HashSet(); <--- a set of (User, Set Roles)
}
---------------------------------------------------
Class User {
......
Set programs = new HashSet();
}
---------------------------------------------------
Class Role {
......
}
---------------------------------------------------
Class UserRole {
User user;
Set roles = new HashSet(); <-- a set of Role for this particular User
}
---------------------------------------------------
And the basic hibernate mapping for <many-to-many> should be:
---------------------------------------------------
<class name="User" table="USERS">
<id ......>
<set name="programs" table="USER_PROGRAM" lazy="true">
<key column="USER_ID"/>
<many-to-many column="PROGRAM_ID" class="Program"/>
</set>
</class>
---------------------------------------------------
<class name="Program" table="PROGRAMS">
<id ......>
<set name="roles" table="PROGRAM_ROLE" lazy="true">
<key column="PROGRAM_ID"/>
<many-to-many column="ROLE_ID" class="Role"/>
</set>
<set name="userRoles" table="PROGRAM_USER_ROLE" lazy="true">
<key column="PROGRAM_ID"/>
<composite-element class="UserRole">
<many-to-one name="user" column="USER_ID" class="User"/>
<many-to-one name="role" column="ROLE_ID" class="Role"/>
</composite-element>
</set>
</class>
----------------------------------------------------
Is it correct ? How the Set of roles in UserRole class is represented in the above mapping ?
thanks in advance !
|