-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to model this : User, Program, and Role
PostPosted: Tue Oct 05, 2004 5:26 am 
Regular
Regular

Joined: Tue Jul 13, 2004 2:27 am
Posts: 73
Location: Singapore
Hi,
Appreciate if you could share with me how to model this in Hibernate:

a USER has many-to-many with PROGRAM;
a PROGRAM has many-to-many with ROLE;
a USER has many-to-many with ROLE.

a USER is assigned a set of ROLEs for each PROGRAM.
^^^^^^^^^^

I think in database, I can have a table with (User_ID, Prog_ID, Role_ID).
How do I define these in Hibernate ?

thanks in advance!
li xin


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 05, 2004 10:12 pm 
Regular
Regular

Joined: Tue Jul 13, 2004 2:27 am
Posts: 73
Location: Singapore
I think I can define the first associations like this (unidirectional):

For class USER:
<set name="PROGRAMS" table="USER_PROGRAM" lazy="true"
cascade="save-update">
<key column="USER_ID"/>
<many-to-many class="program"/>
</set>

class User {
Set programs;
...
}


Similarly, I can have class PROGRAM:
<set name="ROLES" table="PROGRAM_ROLE" lazy="true"
cascade="save-update">
<key column="PROGRAM_ID"/>
<many-to-many class="role"/>
</set>

class Program {
Set roles;
...
}

But how do I define the User and Role association ?
Each User has a subset of Roles assigned to each Program.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 06, 2004 12:56 am 
Regular
Regular

Joined: Tue Jul 13, 2004 2:27 am
Posts: 73
Location: Singapore
Just leant a new term "ternary association". Here is a good link for Hibernate :
http://www.xylax.net/hibernate/ternary.html

So based on this info, I will try to map the class User like this:

<class name="User" table="USERS">
......
<set name="programRoles" table="User_Program_Role">
<key column="USER_ID"/>
<composite-element class="ProgramRole">
<many-to-many name="program" class="Program" column="PROGRAM_ID"/>
<many-to-many name="role" class="Role" column="ROLE_ID"/>
</composite-element>
</set>
......
</class>

Class User {
Set programRoles;

......
}

So now we can:

Set programRoles = user.getProgramRoles();

I hope it works.


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

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.