-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to batch update on many-to-many relationship using HQL?
PostPosted: Fri Nov 28, 2008 12:48 am 
Newbie

Joined: Thu Nov 27, 2008 11:05 pm
Posts: 9
How do you write a *batch* update for a many-to-many relationship? I can't find any documentation or examples on this.

The Contact object has a many-to-many relationship to the Owner object. This is my feeble hql attempt (although i've tried many other variations):

update Contact set Contact.owners.ownerId = :newOwnerId where Contact.owners.ownerId = :ownerId

I get error:
java.lang.IllegalArgumentException: node to traverse cannot be null!

Any guidance would be greatly appreciated!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2008 4:18 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
UPDATE queries in HQL are not allowed to join (explicit or implicit) other entities. This means that you are limited to update column that are part of the root entity of the query. In your case you are limited to update properties on the Contact entity.

But... won't the following HQL work?
Code:
update Owner set ownerId = :newOwnerId where ownerId = :ownerId


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2008 8:06 am 
Newbie

Joined: Thu Nov 27, 2008 11:05 pm
Posts: 9
Thanks for the response. Your proposed HQL won't work in my scenario because the ownerId is the primary key identiy field - that would create two owners with the same identity and I would get a SQL exception.

I guess I'll just loop through the contact's owners or create a native SQL statement for this one.

One thought - would it make sense to map the "join" table in hibernate? Is it possible to update the property of a hibernate object if is part of a compsite key? (e.g. my "join" table ContactOwner only has fields: ownerId and contactId as the composite key)


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