-->
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.  [ 2 posts ] 
Author Message
 Post subject: many-to-many as two one-to-many 's
PostPosted: Tue Sep 21, 2004 6:47 pm 
Newbie

Joined: Tue Sep 21, 2004 6:35 pm
Posts: 2
The Hibernate In Action book recommends that many-to-many relationships are often best mapped as two one-to-many 's in several places. I wish they gave an example..

My question regards how to navigate to the desired set. If I have a many-to-many of User to Roles, how do I get a set of Roles when mapped as two one-to-many 's. When I map a one-to-many from User to UserRole, I get a set of UserRoles, not Users.

Thanks for your help (I tried searching for the answer but didn't find it).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 21, 2004 9:33 pm 
Newbie

Joined: Thu Aug 05, 2004 11:00 pm
Posts: 7
Location: New York, NY
In this situation, once I had a UserRole object, I would usually just call userRole.getUser() or userRole.getRole() to access one entity or the other, depending on which way you are trying to go.

For example, use something like

Code:
Role role = RoleDAO.getById(new Integer(1));
Iterator it = role.getUserRoles();
while (it.hasNext) {
    User user = (User)it.next();
    user.sendEmail("Hi There, " + user.getFirstName());
}



But I'm no expert at all. This may be kludgier than is necessary. Try it and see if it suits your need.

If you want to explore further for something a bit more elegant, you can try putting a many-to-many relationship inside a set or list. But I think this only works in one direction. Hibernate In Action shows (briefly) how to do this.

_________________
When asked by a reporter what he thought of western civilization, Mahatma Ghandi once replied that he thought it would be a good idea.


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