Techspace

IT happens only in IT

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.

December 9, 2008 - Posted by Paras | Hibernate | , , , , | 3 Comments

3 Comments »

  1. This helped me resolve my issue. Thanks a ton

    Comment by viswas | May 21, 2009 | Reply

  2. Thank you , you helped me with my issue. All the best

    Comment by Petarku | July 2, 2009 | Reply

  3. Thanks, saved me a lot of time.

    Best regards,
    Taras Matyashovsky

    Comment by Taras Matyashovsky | October 7, 2009 | Reply


Leave a comment