-->
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.  [ 5 posts ] 
Author Message
 Post subject: Newbie | Mapping Question
PostPosted: Fri Feb 09, 2007 5:35 pm 
Newbie

Joined: Thu Sep 21, 2006 2:08 am
Posts: 7
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3


Hi

Sorry for the lame n dumb question ..

I'm pretty new to hibernate ..actually starting off.. and have this question of how to do mappings.. I did search the forums but couldnt find exactly what I was looking for..

this is the scenario

A 'User' in the webapp can have multiple 'Role's.. say like admin, reviewer etc.

I have 3 classes

User [ userid, name, pwd , roles,.....]
Role [ roleid, description ]
UserRoles [ userid, roleId]


and three tables

User (id, name, pwd.... ) (pl note that theres no 'roles')
Role (roleId, description )
UserRoles( userid, roleId)

I presume that there must be a one-to-many mapping from User to UserRoles and many-to-one from Roles to User


Is this approach correct .. ? Id like to see how mappings look like

edit
===
Do we need this three class approach ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 09, 2007 5:39 pm 
Regular
Regular

Joined: Tue Jan 03, 2006 9:20 am
Posts: 74
actually a many to many mapping might make more sense.

A user can have multiple roles, but each of those roles would apply to many users.

And I'm sure there will come a time when you need a list of all users with a specific role, rather than all roles for a specific user.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 10, 2007 2:18 pm 
Newbie

Joined: Thu Sep 21, 2006 2:08 am
Posts: 7
Thanks.. Im not trying to quiz you, but I'm not able to get this .. why should a one-to-many relation be tweaked to become a many-to-many ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 12, 2007 10:47 am 
Regular
Regular

Joined: Fri May 12, 2006 4:05 am
Posts: 106
Hi,

I think jwenting is right - your UserRoles-Class doesn't make that much sense form an object-oriented point of view. The only "real" business-objects are the Users and their Roles and these have a many-to-many-relationship. The UserRoles are some artificial construct needed to represent the many-to-many-relationship in a relational model - but why should you introduce this into an object-model where it isn't needed (if you do need it except for representing the relationships you might as well forget this post).
Overall, we aren't tweaking a one-to-many-relation to become many-to many. You're tweaking an existing many-to-many-realtionship to become two seperate relations (one-to-many and many-to-one).
So I'd also suggest you eliminate your UserRoles-class (if possible) and model a many-to-many-relationship between User and Role classes.
You will still need the UserRoles-table(!) as an association-table!

Greets

piet


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 12, 2007 3:51 pm 
Newbie

Joined: Thu Sep 21, 2006 2:08 am
Posts: 7
thnx a tonne piet .. that explains it


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.