-->
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.  [ 4 posts ] 
Author Message
 Post subject: many-to-many problem
PostPosted: Sun Oct 12, 2003 2:24 pm 
Newbie

Joined: Tue Sep 30, 2003 9:27 am
Posts: 12
Hi Guys,

Say I have Users and Roles in a bi-directional many-2-many relationship. (there is no ownership (ie. roles can exist independent of users and vice versa).

How do I go about adding a role to a user in a web scenario? You see, in this scenario I would have, in my action class, only he userID and the roleID. Now I want to add the role to the user (and vice versa). I don't know how to do this.

In SQL, it would be easy. Simply add a row to the UserRoleLink (the linking table) table.

But in Hibernate I have to think differently, and I don't quite know what to do here.

I could: find the user and get the User object. Likewise for Role object. Update the role collection in User (u.getRoles().add(r), r.getUsers().add(u). Ask Hibernate to "update" both objects. That sounds awfully expensive - a lot more than a simple SQL update?

How is one supposed to do this in an O-R scenario? Am I supposed to materialise the objects again and work on them? Doesn't this cause many superflous fetches?

Thanks
J

[/list]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 14, 2003 9:05 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
I could: find the user and get the User object. Likewise for Role object. Update the role collection in User (u.getRoles().add(r), r.getUsers().add(u). Ask Hibernate to "update" both objects.

Thats exactly how you would do it.

Quote:
That sounds awfully expensive - a lot more than a simple SQL update?

If the classes are mapped with proxies, session.load() will not actually materialize the entity, just return the proxied instance.

You are not dealing with SQL, you are dealing with objects. Thats the whole point behind O/R mapping. I mean if you really want to directly issue SQL statements then do it. Or if you want to work with an object model instead, then use O/R layer.


Top
 Profile  
 
 Post subject: Spot on
PostPosted: Wed Oct 15, 2003 8:47 am 
Newbie

Joined: Tue Sep 30, 2003 9:27 am
Posts: 12
Hi Steve,

Yes - that is what I am learning. I do not want to use SQL - I want to do it the "proper" way. It's trying to find out what the "proper" way is - I read the Hibernate documentation, and even a book on JDO - but neither explained how to go about doing this. The recommended method, as it were.

I'll look into the proxied business. Currently, I notice from my logs that it does perform the read.

Thanks for your reply,
Jeff


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2003 4:32 pm 
Newbie

Joined: Fri Oct 03, 2003 1:10 am
Posts: 10
After some experimenting, I've come to believe the following about many-to-many's:

1. It is not necessary to update both sides (u.getRoles().add(r), r.getUsers().add(u)) unless you are planning to navigate the association from both sides in the current session. You can update just one side (as long as it is not specified with inverse="true"), and the database will be updated correctly. When you come back to use the association in a subsequent session, you will get correct collections from either side.

2. Specify inverse="false" on both sides of the realtionship if you want to be able to define the collection from either side. This seems to be the best way to define all many-to-many's.


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