-->
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: trouble updating a many-to-many relationship via HQL
PostPosted: Thu Jun 14, 2012 8:48 pm 
Newbie

Joined: Thu Jun 14, 2012 8:42 pm
Posts: 2
I'm really stuck on something that seems like it should be easy. I have a Spring MVC webapp using Hibernate version 3.6.6. I have two objects - "ProjectUser" and "Project" - with a many-to-many relationship described in the database by the tables "users, "projects", and "projects_users." I have a portion of my app where I want to update the users in a project without updating any other fields in the "projects" table (it's a big table with lots of fields), so the easiest thing seemed to be to just write a separate method in my DAO and call the HQL directly. Here's the relevant portion of the "projects" class:

Code:
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "projects_users", joinColumns = @JoinColumn(name = "project_id"), inverseJoinColumns = @JoinColumn(name = "user_id"))
private Set<ProjectUser> projectUsers ;


Everything works just fine on a select and also works if I pass along a full Project object and just call "session.saveOrUpdate(project)." However, my HQL to just update the users clearly has problems. Here's what I'm executing now:

Code:
Query query = getSession().createQuery("update Project set projectUsers = :projectUsers where id = :projectID ");
query.setParameter("projectUsers", project.getProjectUsers());
query.setParameter("projectID", project.getId());
int result = query.executeUpdate();


When I run this, I get a SQLGrammarException that reads "no value specified for parameter 2." It looks like I have a syntax error when I'm passing through the Set of ProjectUsers (if I run this same code on another field that does not involve a join it all works fine), but I can't figure out what to change to make this work. Any help would be appreciated.

Thanks,
Alex


Top
 Profile  
 
 Post subject: Re: trouble updating a many-to-many relationship via HQL
PostPosted: Fri Jun 15, 2012 2:25 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
It's not possible to update collections with HQL. I think you'll have to use native SQL and work directly with the project_users table.


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.