-->
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.  [ 5 posts ] 
Author Message
 Post subject: Simpler HQL-Queries with "WHERE...IN (...)"
PostPosted: Fri Nov 19, 2004 12:21 pm 
Newbie

Joined: Mon Sep 20, 2004 7:06 am
Posts: 5
Hello!

I often use queries like
Code:
FROM Person person WHERE person.name IN ('joe', 'frank', 'andy')
where the names are retrieved from a collection or array. Currently i use a method to quote the strings and insert "," in between, so i construct the string "'joe', 'frank', 'andy'" manually.

Is there any simpler way to do this (without using the criteria api, just by plain written hql)? The criteria api supports
Code:
Expression.in("name", new String[] {"joe", "frank", "andy"} );


I thought of something like
Code:
hibernateTemplate.find("FROM Person person WHERE person.name IN ?", new String[] {"joe", "frank", "andy"} );

(which actually does not work, but causes an error because hibernate is looking for a second and third string parameter in the query)

Is there any way to do this?
I found elements() in "Hibernate in Action", but it seems elements() can not be used on Collections or Array given as parameters.

Thanks for your help!
Sebastian


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 12:22 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
"where name in ?", query.setParameterList(myList);

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 1:22 pm 
Newbie

Joined: Mon Sep 20, 2004 7:06 am
Posts: 5
Wow, now that was a quick answer - faster than i can close my browser window after submitting my question... ;-)

Thanks, I am mainly using Spring's HibernateTemplate ans was not aware of this method.

Thanks again!
Sebastian


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 19, 2004 1:25 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Yeah, thats the danger if you decide to hide a perfectly clean user API behind another one. I wouldn't do it.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 29, 2004 1:02 pm 
Newbie

Joined: Fri Aug 13, 2004 12:49 pm
Posts: 10
Just for the record - Spring will automatically use setParameterList() if the parameter value passed in is a Collection.

------
Thomas


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