-->
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: Modelling a "many to many to many" relationship.
PostPosted: Fri Jan 23, 2009 5:46 pm 
Newbie

Joined: Fri Jan 23, 2009 5:38 pm
Posts: 3
Hibernate version: 3.2.6

Mapping documents: None yet.

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

I'm looking for advice on how to model a system.

I have Users, Events, and Roles.

Users control many Events, and Events can be controlled by many Users.

Roles restrict how much control a User has over a given Event. Users can have many roles (per Event). Roles are used by many Users.

The Roles table will have a mostly-unchanging list of Roles, for example: "view_widgets", "print_reports", "edit_attendants", basically an ACL of possible things a User can do to an Event.

So I want a User to be associated with many Events, and for each of those Events, have a set of Roles.

How best to describe this in terms of database table structure? A single join table, or two?

How best to describe this in Hibernate using annotations to describe the relationships between User, Event and Role?

Note: A User can only control one Event at a time, in the web interface.

Who needs to know what:

Users need to know
-- Which Events can I control?
-- Which Roles do I have for the Event I'm currently controlling

Events need to know
-- Which Users can control me?

Roles don't need to know anything.

Any responses are much appreciated, even to tell me that I shouldn't be doing what I've described so far!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 23, 2009 6:07 pm 
Newbie

Joined: Fri Jan 23, 2009 5:38 pm
Posts: 3
Without Hibernate, I would do something like:

User:
Code:
id | name
---+-----
1  | eric
2  | john


Event:
Code:
id | name
---+-----
1  | golf trip
2  | tennis trip


Role:
Code:
id | name
---+-----
1  | add_attendant
2  | rename_event
3  | apply_payment


Using a single join table:

Code:
uid | eid | rid
----+-----+----
1   | 1   | 1
1   | 1   | 3


This would say "eric can add attendants and apply payments to the golf trip".

How do I implement this using Hibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 23, 2009 8:04 pm 
Newbie

Joined: Fri Jan 23, 2009 5:38 pm
Posts: 3
Here's maybe a more direct question. How can I model a Map which is keyed by an entity, and each value is a Set of other entities?

Code:
public class User {
  private Map<Event, Set<Role>> roles;
}


So for a User u, and Event e,

Code:
u.getRoles().keySet() // Set of events this User can control
u.getRoles().get(e) // Set of Roles a User has for a particular Event


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.