-->
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.  [ 8 posts ] 
Author Message
 Post subject: Using List in HQL WHERE clause
PostPosted: Sun Feb 17, 2008 1:50 pm 
Newbie

Joined: Sun Feb 17, 2008 1:38 pm
Posts: 3
Basically I want to pass a List into HQL to filter by. I cannot seem to find an example on the forums passing in a List, just hard coded values in the HQL.

Any help greatly appreciated,

Hibernate version:
3.2.5ga

Code between sessionFactory.openSession() and session.close():
Using spring 2.5

Full stack trace of any exception that occurs:
org.hibernate.hql.ast.QuerySyntaxException: expecting IDENT, found '?' near line 1, column 168 [select l from Location as l where l.latitude = ? and l.longitude = ? and l.category.code in elements(?)]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:31)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:24)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:258)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:157)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at org.springframework.orm.hibernate3.HibernateTemplate$29.doInHibernate(HibernateTemplate.java:849)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:373)
Name and version of the database you are using:
PostgreSQL 8.2.4
The generated SQL (show_sql=true):

select l from Location as l where l.latitude = ? and l.longitude = ? and l.category.code in elements(?)


Top
 Profile  
 
 Post subject: Re: Using List in HQL WHERE clause
PostPosted: Sun Feb 17, 2008 2:50 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Have you tried Query.setParameterList(...)?


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 18, 2008 4:37 am 
Newbie

Joined: Tue Mar 15, 2005 7:37 am
Posts: 16
Or just simply use:

Code:
select l from Location l where l.category.code in (:codes)


/Magnus


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 18, 2008 1:40 pm 
Newbie

Joined: Sun Feb 17, 2008 1:38 pm
Posts: 3
Same deal.

expecting IDENT, found ':' near line 1, column 175 [select l from Location as l where l.latitude = :lat and l.longitude = :long and l.category.code in elements(:codes)]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: expecting IDENT, found ':' near line 1, column 175 [select l from Location as l where l.latitude = :lat and l.longitude = :long and l.category.code in elements(:codes)]
org.springframework.orm.hibernate3.HibernateQueryException: expecting IDENT, found ':' near line 1, column 175 [select l from Location as l where l.latitude = :lat and l.longitude = :long and l.category.code in elements(:codes)]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: expecting IDENT, found ':' near line 1, column 175 [select l from Location as l where l.latitude = :lat and l.longitude = :long and l.category.code in elements(:codes)]
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:640)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:378)
at org.springframework.orm.hibernate3.HibernateTemplate.findByNamedParam(HibernateTemplate.java:873)
at com.tradecraft.capetown.server.dao.impl.hibernate.CapeTownDao.getLocations(CapeTownDao.java:151)
at com.tradecraft.capetown.server.dao.impl.hibernate.test.GetLocationsTest.testSuccessfulLocationRetrieval(GetLocationsTest.java:71)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76)
Caused by: org.hibernate.hql.ast.QuerySyntaxException: expecting IDENT, found ':' near line 1, column 175 [select l from Location as l where l.latitude = :lat and l.longitude = :long and l.category.code in elements(:codes)]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:31)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:24)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:258)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:157)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:875)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:373)


emsa wrote:
Or just simply use:

Code:
select l from Location l where l.category.code in (:codes)


/Magnus


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 18, 2008 1:42 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
don't use elements. You don't need it.



Farzad-


Top
 Profile  
 
 Post subject: still no go
PostPosted: Mon Feb 18, 2008 2:53 pm 
Newbie

Joined: Sun Feb 17, 2008 1:38 pm
Posts: 3
ClassCastException being thrown. HQL is expecting a String not a List.

farzad wrote:
don't use elements. You don't need it.

Farzad-


Top
 Profile  
 
 Post subject: Re: still no go
PostPosted: Mon Feb 18, 2008 2:55 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
bobby.quninne wrote:
ClassCastException being thrown. HQL is expecting a String not a List.



That's where you need to set the list parameter with setParameterList(..)


Farzad-


Top
 Profile  
 
 Post subject: Re: Using List in HQL WHERE clause
PostPosted: Fri Apr 30, 2010 2:09 pm 
Newbie

Joined: Fri Apr 30, 2010 2:00 pm
Posts: 1
hi have a doubt....

i'm using a list param in my query like this:

Code:
select aula
from Aula as aula,
aula.tags as tag
where tag in :tags


my problem is: How to order these results by the AULAs that has the most number of TAGs in :tags list?

thanks for your atention! :D


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