-->
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.  [ 9 posts ] 
Author Message
 Post subject: delete the joiner in a many-to-many
PostPosted: Mon Oct 24, 2005 10:59 pm 
Beginner
Beginner

Joined: Thu Feb 03, 2005 10:42 pm
Posts: 30
I am sure there is a way to do this but I can't figure it out.
I have a lot of cases where I have a many-to-many and I want to delete the joiner.
So for instance I have three tables:
USER (id , username, password)
GROUP(Id, name)
USER_GROUP_JOINER(user_id, group_id)

A user has a whole bunch of groups attached to it:

<class name="User" table="USER" lazy="false">
<id name="id" column="ID" type="int" unsaved-value="-1">
<generator class="identity"/>
</id>
<property name="userName" column="USERNAME" not-null="true"/>
<property name="password" column="PASSWORD" not-null="true"/>
<set name="roleGroups" table="USER_GROUP_JOINER">
<key column="USER_ID"/>
<many-to-many column="GROUP_ID" class="Group"/>
</set>
</class>

I have functional groups and sometimes require to detach all of a certain type of group from every user. If it was plain sql i would say delete from USER_GROUP_JOINER where group_id =?.

How do you do this with hibernate cleanly?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 25, 2005 2:44 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
Read the part about "Don't use exotic association mappings" . There are a lot of reason why, but in your case, it would be as simple as Session.delete(myJoinerObject) .

http://www.hibernate.org/hib_docs/v3/re ... tices.html


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 25, 2005 12:45 pm 
Beginner
Beginner

Joined: Thu Feb 03, 2005 10:42 pm
Posts: 30
dennisbyrne wrote:
Read the part about "Don't use exotic association mappings" . There are a lot of reason why, but in your case, it would be as simple as Session.delete(myJoinerObject) .

http://www.hibernate.org/hib_docs/v3/re ... tices.html


This doesn't seem right. It seems overkill to me to have to create an object for all my joiners. And also user has a set of groups objects not a set of user_Group_joiner objects. So where do I get the my joiner object from?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 25, 2005 1:14 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
wexwarez wrote:
dennisbyrne wrote:
Read the part about "Don't use exotic association mappings" . There are a lot of reason why, but in your case, it would be as simple as Session.delete(myJoinerObject) .

http://www.hibernate.org/hib_docs/v3/re ... tices.html


This doesn't seem right. It seems overkill to me to have to create an object for all my joiners. And also user has a set of groups objects not a set of user_Group_joiner objects. So where do I get the my joiner object from?


Just add cascade="all" to the <set> mapping and if you remove the associated object from the Parent collection, and then update the Parent, the association will be deleted.

However, for the type of Query you are talking about - "detach all of a certain type of group from every user" - I think that using straight SQL still makes more sense.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 25, 2005 1:39 pm 
Beginner
Beginner

Joined: Thu Feb 03, 2005 10:42 pm
Posts: 30
Ok so I am gathering that I have essentially 3 options.

1)Use a straight sql query to delete the joiners.

2)Create an object representing the USER_GROUP_JOINER table, map it, and then I can use hsql to delete these objects.

3)Query all the users that have the group in question, then iterate through the results, and remove remove the group and save user on a one by one basis.

The first option is easy but I would rather stick fully within the hibernate framework. The second option seems pretty clean just a lot of extra work creating the class and mapping. The third option seems like a performance nightmare if you perform this type of operation often. So what is the best one? Am I leaving something out?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 25, 2005 1:46 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
I would look at the second two. note that I don't think you find many people w/ performance problems doing this, how many groups could one user be in?

at the same time, you will have to go w/ number 2 in the event that you ever need non-key information from the association table. for example, you may get a requirement to track when a user joined a group, or who added that user to the group, etc.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 25, 2005 1:46 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
wexwarez wrote:
Ok so I am gathering that I have essentially 3 options.

1)Use a straight sql query to delete the joiners.

2)Create an object representing the USER_GROUP_JOINER table, map it, and then I can use hsql to delete these objects.

3)Query all the users that have the group in question, then iterate through the results, and remove remove the group and save user on a one by one basis.

The first option is easy but I would rather stick fully within the hibernate framework. The second option seems pretty clean just a lot of extra work creating the class and mapping. The third option seems like a performance nightmare if you perform this type of operation often. So what is the best one? Am I leaving something out?


I think you have your 3 options correct but I wouldn't abandon option 1 so quickly. The Hibernate team realizes that there are valid use-cases for bulk updates and provide a way for doing this through Hibernate. It is a major emphasis in the H3.1.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 25, 2005 2:13 pm 
Beginner
Beginner

Joined: Thu Feb 03, 2005 10:42 pm
Posts: 30
dennisbyrne wrote:
I would look at the second two. note that I don't think you find many people w/ performance problems doing this, how many groups could one user be in?

at the same time, you will have to go w/ number 2 in the event that you ever need non-key information from the association table. for example, you may get a requirement to track when a user joined a group, or who added that user to the group, etc.


Thanks a lot. In terms of performance, you are correct that in this scenario I don't think it would be too bad. But I was just using the user/group as an example of this type of association, which I use pretty often so I was worried about nailing down the best general use technique to attack this problem. This is my first time implementing in hibernate and I am converting a big application so I am still trying to figure out some of these options.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 25, 2005 2:15 pm 
Beginner
Beginner

Joined: Thu Feb 03, 2005 10:42 pm
Posts: 30
pksiv wrote:
wexwarez wrote:
Ok so I am gathering that I have essentially 3 options.

1)Use a straight sql query to delete the joiners.

2)Create an object representing the USER_GROUP_JOINER table, map it, and then I can use hsql to delete these objects.

3)Query all the users that have the group in question, then iterate through the results, and remove remove the group and save user on a one by one basis.

The first option is easy but I would rather stick fully within the hibernate framework. The second option seems pretty clean just a lot of extra work creating the class and mapping. The third option seems like a performance nightmare if you perform this type of operation often. So what is the best one? Am I leaving something out?


I think you have your 3 options correct but I wouldn't abandon option 1 so quickly. The Hibernate team realizes that there are valid use-cases for bulk updates and provide a way for doing this through Hibernate. It is a major emphasis in the H3.1.


Thanks. My problem with option 1 is one of the reasons I am working with hibernate on this particular application is that I will be offering it with two different databases and I am trying to stay db independant by sticking with hsql. Thanks for your help.


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