-->
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: Many to Many & Extra Attributes
PostPosted: Tue Nov 04, 2003 11:36 am 
Newbie

Joined: Fri Oct 31, 2003 11:51 am
Posts: 3
I have just started using Hibernate and have successfully setup a many to many relationship. I have one small problem though...

I have three tables, user, role and user_role. user_role joins user and role in the many to many relationship. In my User class the getRoles() method returns a Set of roles assigned the given User. However the user_role table has a column which indicates which is the default role. How do I get this attribute, which is in the user_role table, into the Role class (which maps to the role table).

TIA


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2003 12:16 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
You can create a class for the user_role table ...

Code:
public class UserRole {
   User user;
   Role role;
   boolean default;
   .....
}

<class name="User" table="user">
   ....
   ....
   <set name="roles" table="user_role" lazy="true">
      <key>
      <column name="user_id" not-null="true"/>
   </key>
   <one-to-many class="UserRole"/>
    </set>
   
</class>


<class name="UserRole" table="user_role">

    <composite-id>
       <key-many-to-one name="user" class="User" column="user_id"/>
   <key-many-to-one name="role" class="Role" column="role_id"/>
   </composite-id>

        <property name="default"/>
     
   
</class>


I think that exists another way to implement but I use this.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2003 8:18 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
We actually use explicit modelling of many-many relationships as a general approach in our application.
We usually have a minimum of at least four extra auditing attributes (userCreated, userUpdated, etc) and often more - a type mapping to a reference table (making the whole thing a ternary mapping), a primary role indicator like yours; and sometimes other stuff.

We also always map our many-many "relationship" classes with a surrogate key (completely seperate from the foreign keys the table exists to model) as well!

I know it sounds horrible, and I was really against it at first - because I thought that it was wasting half the potential of an OR/Mapper - ie. transaparent mapping of collections. I thought it would be quite tedious to implement, in terms of having to write the relationship classes.

But it turns out not to have been much of an issue so far. It makes it easy to teach non-OR/Mapping people how to map many-many relationships, and it kind of makes it explicit for them so they can see what's going on instead of trying to imagine (and make wrong assumptions about) what Hibernate is doing.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2003 8:23 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
We used the same approach on our last project. Its not horrible at all - in fact I think it is probably a best practice.

But in 2.1, composite elements do offer a really great alternative solution to the problem above.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2003 4:18 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
gavin - please put an example of this (maybe even in context of the concrete model)....that would be great to have in the same context as this thread....

_________________
Max
Don't forget to rate


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.