-->
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.  [ 7 posts ] 
Author Message
 Post subject: Using IN Clause in HQL
PostPosted: Fri May 04, 2007 1:54 pm 
Newbie

Joined: Fri May 04, 2007 1:48 pm
Posts: 7
Hi All,

I am new to hibernate. My project already has hibernate implementation. I have introduce a new query which should use a IN clause.

For E.g.,

SELECT * FROM FEATURE WHERE FEATURE_NAME IN ('TAX','GEO_CODE');

I will be getting the values in the IN clause in a collection in my code.
How should i pass the value to the HQL query?
How will the HQL query look like?

Thanks,
Balaji


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 04, 2007 2:26 pm 
Regular
Regular

Joined: Wed Nov 17, 2004 11:49 am
Posts: 65
Location: Pittsburgh
Code:
session.createCriteria(Feature.class).add(Restrictions.in("name", names:Collection<String>)


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 04, 2007 2:27 pm 
Regular
Regular

Joined: Wed Nov 17, 2004 11:49 am
Posts: 65
Location: Pittsburgh
Code:
"from Features.class where name in :names".setParameter("names", names:Collection<String>).list()


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 04, 2007 5:46 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
try something like

Code:
Session.createQuery("from Feature f where f.name in (:names)").setParameterList("names",vals).list()

where vals is a Collection

_________________
nathan


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 10:23 am 
Newbie

Joined: Fri May 04, 2007 1:48 pm
Posts: 7
Hi,

Thanks for ur help.

I am getting the following ClassCastException when i have the query just as the way you have mentioned. To be clear following is the code.

Collection featureList = new ArrayList();
featureList.add("GET_TAX");
featureList.add("GET_GEO_CODE");
Query query = HibernateUtil.getCurrentSession().getNamedQuery(
"Feature.findByFeatureNames");
query.setParameter("featureNames", featureList);
List outList = query.list();

Following is the query in my HBM file.

<query name="Feature.findByFeatureNames">
<![CDATA[SELECT a FROM FeatureHBImpl AS a WHERE a.featureCode in (:featureNames)]]>
</query>

I am getting the ClassCastException in this line.

List outList = query.list();

Please let me know if I am missing anything.

Thanks
Balaji


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 11:47 am 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
you need to use setParameterList, not setParameter

_________________
nathan


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 12:10 pm 
Newbie

Joined: Fri May 04, 2007 1:48 pm
Posts: 7
Hi Nathan/Kerstter,

Yep. That's what i missed.

I got it to work now. Thanks for ur help.

Balaji


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