-->
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: questions on only remove relationship between many-to-many
PostPosted: Fri Aug 18, 2006 2:04 am 
Newbie

Joined: Thu Aug 17, 2006 11:23 pm
Posts: 1
hi,all!
I am a newbie in hibernate.
Now, I come across one problem.
I want to remove the relation between two entities only. Take for example, I have two entities such as User and Group. They have a many-to-many relationship. Now I want to remove a user from the group. One solution is to load group's user colletion and remove related user object and saveOrUpdate group object finally. But it is not very good. Because it generates many sql statements which will affect the performance greatly. I think it better to write one hql statement instead.
Who can tell me how to write this hql statement?
Thanks a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 20, 2006 9:57 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You cannot do it in HQL as the join table is not mapped as an entity. You can do it using an SQL query easily enough:
Code:
<sql-query name="RemoveAssocFromUnloadedEntities">
  delete from User_Group where userid = :userid and groupid = :groupid
</sql-query>
Then load this query and call executeUpdate. I can't remember if one of the <return*> tags is mandatory or not, but you should be able to get it working quite quickly.

Note that if you do this and you have either the user or the group in memory at the time, you'll have to refresh them afterwards, essentially wasting the performance benefit of running the query. So I suggest using the normal technique (group.getUsers().remove(user), etc.) when you have one or more of the relevant entities loaded, and only resort to the sql-query when neither entity is in memory.

_________________
Code tags are your friend. Know them and use them.


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.