-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: need help: ternary association using composite-element
PostPosted: Wed Oct 06, 2004 9:28 pm 
Regular
Regular

Joined: Tue Jul 13, 2004 2:27 am
Posts: 73
Location: Singapore
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 !


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.