-->
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: manu-to-many OR one-to-many
PostPosted: Fri Feb 03, 2006 3:52 am 
Newbie

Joined: Thu Feb 02, 2006 10:14 am
Posts: 9
What kind of associations should I configure ? (I've 2 ideas: create one: many-to-many or two: one-to-many)

In my situation I've 3 tables in database:

a) user: - table of Users
user_id name
1______jack
2______mary
3______zigi
so there are 3 users in database

b) role: - table of Roles
role_id name
1______admin
2______editor
so there are 2 roles in database

c) user_role - association table from User to Role and from Role to User
user_id role_i
1______1
2______2
3______1
3______2
so user "jack" is "admin", "mary" is "editor", but "zigi" is "admin" and "editor"

So every user belongs to one or more roles in some system.

What I need are:

I) when User (with some user_id) or Role (with some role_id) is removed from database: session.delete(User) or session.delete(Role) - the associations in table (c) which belond to given User or Role are also removed, but only the associations, not object on the second end of associations for exapmle, if I remove User whith user_id 3 the action made in database are: removing row from table (a) with user_id = 3 and remove all rows from table (c) whit rows user_id = 3 and don't remove any row from table (b)

II) when we add or remove some user's role for example whe don't whant any more that "zigi" (user_id = 3) could be admin I whant to remove this only from association table (c), so the action made in database is only to remove from table (c) all rows with user_id = 3 and don't remove any row from table (b) and talbe (a)


Hibernate version:
3.0

Name and version of the database you are using:
MySQL 4.1.15


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 03, 2006 4:47 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
Sounds like many to many to me with users having a collection of roles and vice versa. When you delete associations (scenario II), you just modify the Collections.

E.g.

Code:
String[] rolesToRemove = {"Admin", "Moderator"};
Collection roles = user.getRoles();
removeFromCollection(roles, rolesToRemove); // some helper method you write
user.setRoles(roles);


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 7:39 am 
Newbie

Joined: Thu Feb 02, 2006 10:14 am
Posts: 9
Thnx - everything works great :) Hibernate is realy powerfull :)


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.