-->
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: HQL query help
PostPosted: Sun Jan 29, 2006 1:16 pm 
Beginner
Beginner

Joined: Thu Aug 04, 2005 8:41 pm
Posts: 47
Hello,

I have a list (ArrayList<String>) of String in java and I want to pass it to HQL query
into 'in' condition of where clause.
I've tried

Code:
select e.id from Event as e where e.eventRef in (?)


Code:
select e.id from Event as e where e.eventRef in elements(?)


Can't seem to figure out how to do it properly.
In DAO I'm passing list using query.setEntity(0, refsList)
where refsList is of type ArrayList<String>.
Your help is greatly appreciated.

I'm using hibernate 3.1.1

Thanks a lot,
--MG


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 29, 2006 2:45 pm 
Beginner
Beginner

Joined: Sun Jan 22, 2006 6:56 am
Posts: 29
I'm assuming Event.eventRef is a String.

You could do the following:

Code:
String queryStr = "select e.id from Event as e where e.eventRef in (";
String[] questionMarks = new String[refsList.size()];
Arrays.fill(questionMarks, "?);
queryStr += StringUtils.join(questionMarks, ",");
queryStr += ")";


Where StringUtils.join (from apache-commons) takes all the elements in the array and concatenates them with commas.

By the way if you're using Spring, I think you can pass a List as a parameter value and it gets handled correctly (not sure).

Also you could use the Criteria API:

Code:
Criteria c = Session.createCriteria(Event.class);
c.add(Restrictions.in("eventRef", refsList);
c.list();


Top
 Profile  
 
 Post subject: HQL parameterising 'where in'
PostPosted: Sun Jan 29, 2006 9:39 pm 
Beginner
Beginner

Joined: Thu Aug 04, 2005 8:41 pm
Posts: 47
Thanks,

I got it working with Criteria, Restriction, and Projection
--MG


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.