tags:

views:

33

answers:

2

How best to use hibernate in a simple scenario (questions at end of the post)

Lets say I have this Data Model in code:

 class UserGroup
 {
 int id;
 String name;
 }

 class User
 {
 int id;
 String userName;
 }

For the User and UserGroup classes, the underyling database table will look exactly as defined by the classes above.
Now, a User can belong to one or more UserGroups.

In a database table called USERGROUPMAP, this can be represented as:

USERID INT
USERGROUPID INT

In code, how will I represent that relation? The options I can think of are:

Option 1: (The UserGroup class contains a collection of the users belonging to it)

 class UserGroup
 {
 int id;
 String name;
 UserCollection usersThatBelongInThisGroup;
 }

Option 2: (The User class contains a collection of groups it belongs to)

 class User
 {
 int id;
 String name;
 UserGroupCollection groupsThatIBelongTo;
 }

Option 3: (A class that represnts a database table called USERGROUPMAP. In order to find out if a user belongs to a group etc, this class will need to be checked in-code)

 class UserGroupMap //Class that represents the USERGROUPMAP table.
 {
 int userId;
 int userGroupId;
 }

The questions:

(A) If I use the 3rd option, then where is the benefit of using hibernate (after all its just another class representing a table)? I mean, I should be able to define a collection in class Users or UserGroup, and hibernate should take care of the insert/updates in USERGROUPMAP for me. So I think that rules out Option 3.

(B) If I use the 1st option, then whats the point of holding information about groups in the 2nd option? (since it can become redundant)

(C) Similarly, if I use the 2nd option then whats the point of the collection of users in the 1st option?

How is this to be tackled in a ORM like hibernate?

A: 

To me the first question is how do you actually use your objects. Are you going to need to lookup the groups for a user more often, or the users for a group? IMO that should decide how you map your entities.

Hibernate allows you to use both 1) and 2) together. However, it is also easy to write a separate query to fetch 2) if you only implement 1) in code (or vice versa). And of course, if you implement both 1) and 2), you need to be careful with deviating from the default lazy fetching strategy - you can easily end up loading all your users and groups with a single eager fetching query.

Péter Török
Its a CRUD type application, so I cant predict which will be used more often - it depends on what the user selects - eg dragging 5 user into a group or dragging 5 group onto a user... do I have to use only one of option 1/2 and cant use both?
frissony
@frissony I updated my answer
Péter Török
A: 

Option 4 (and how I believe Hibernate does it):

 class UserGroup 
 { 
   int id; 
   String name; 
   UserCollection usersThatBelongInThisGroup; 
 } 

 class User 
 { 
   int id; 
   String name; 
   UserGroupCollection groupsThatIBelongTo; 
 }

Generally, the collections are lazy loaded and only accessed when needed, although they can be eager loaded if needed.

Michael Maddox