Hibernate error : org.hibernate.hql.ast.QuerySyntaxException: unexpected end of subtree
I got this error today.
Caused by: org.hibernate.hql.ast.QuerySyntaxException: unexpected end of subtree [select count(*) from com.test.Fruits as fruit where fruit.fruitId in () order by fruit.fruitName desc ] 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.analyze(QueryTranslatorImpl.java:235) at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160) 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.SessionImpl.list(SessionImpl.java:1113) at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79) at com.test.FruitDAOServiceImpl.fruitCount(FruitDAOServiceImpl.java:682) at jrockit.reflect.VirtualNativeMethodInvoker.invoke(Ljava.lang.Object;[Ljava.lang.Object;)Ljava.lang.Object;(Unknown Source) at java.lang.reflect.Method.invoke(Ljava.lang.Object;[Ljava.lang.Object;I)Ljava.lang.Object;(Unknown Source) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:139) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)</code>
While there could be many reasons because of which this error comes. For me, the problem was that the parameter list I was passing was empty.
I was getting all the fruits for which fruit id is in the fruitIdList I was passing as the query parameter. If the fruitIdList is emply list then this problem comes. I solved it by conditionally framing query based on the list. That is instead of
public Long fruitCount(List<Long> fruitIdList){
........
Query fruitQuery = getSession().createQuery("select count(*) from com.test.Fruits as fruit where fruit.fruitId in (:fruitIdList) order by fruit.fruitName desc ");
fruitQuery.setParameterList("fruitIdList", fruitIdList);
List<Long> fruitCountList = fruitQuery.list();
..........
}
I did this
public Long fruitCount(List<Long> fruitIdList){
........
StringBuffer queryString = new StringBuffer("select count(*) from com.test.Fruits as fruit ");
if(fruitIdList !=null && !fruitIdList.isEmpty()){
queryString.append(" where fruit.fruitId in (:fruitIdList) ");
}
queryString.append(" order by fruit.fruitName desc ");
Query fruitQuery = getSession().createQuery(queryString.toString());
if(fruitIdList !=null && !fruitIdList.isEmpty()){
fruitQuery.setParameterList("fruitIdList", fruitIdList);
}
List<Long> fruitCountList = fruitQuery.list();
..........
}
In short, append the collection parameter in the query and the query parameter only if the collection is non-empty.
3 Comments »
Leave a comment
-
Archives
- October 2009 (2)
- September 2009 (3)
- August 2009 (2)
- July 2009 (1)
- June 2009 (2)
- May 2009 (3)
- April 2009 (8)
- February 2009 (2)
- December 2008 (1)
- November 2008 (3)
- October 2008 (1)
- January 2008 (8)
-
Categories
-
RSS
Entries RSS
Comments RSS

This helped me resolve my issue. Thanks a ton
Thank you , you helped me with my issue. All the best
Thanks, saved me a lot of time.
Best regards,
Taras Matyashovsky