Hibernate error : org.hibernate.hql.ast.QuerySyntaxException: unexpected end of subtree

Update: Look at the comments for more information and possible solutions.

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.

8 thoughts on “Hibernate error : org.hibernate.hql.ast.QuerySyntaxException: unexpected end of subtree

    1. Steve J

      Thanks for the bug tracking link. Paul Benedict posted this over there:
      “My workaround is to provide a sentinel value that will never match. For example, -1 for a primary key.”
      By adding a -1, your list will never be empty and you can avoid this issue.

      Reply
  1. jay

    I’m getting this type of error on excuting sql query.
    What should i do?

    org.hibernate.hql.ast.QuerySyntaxException: px_assetinput is not mapped [Select pxcol_shopname,pxcol_signage,pxcol_hordingsize From px_assetinput Where pxcol_latitude=’78.123121′ And pxcol_longitude=’19.324234′]
    at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
    at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
    at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
    at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:255)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
    at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
    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.(HQLQueryPlan.java:77)
    at org.hibernate.engine.query.HQLQueryPlan.(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 iworktech.gmap.webservice.hibernate.dataaccessframework.HibernateAssetManager.funcGetAsset(HibernateAssetManager.java:91)
    at iworktech.gmap.webservice.manager.AssetManager.funcGetAssetDetails(AssetManager.java:396)
    at iworktech.gmap.webservice.manager.AssetManager.GetAssetDetails(AssetManager.java:324)
    at iworktech.gmap.webservice.service.GmapManagerImpl.funcGetGmapData(GmapManagerImpl.java:69)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.xfire.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:54)
    at org.codehaus.xfire.service.binding.ServiceInvocationHandler.sendMessage(ServiceInvocationHandler.java:271)
    at org.codehaus.xfire.service.binding.ServiceInvocationHandler$1.run(ServiceInvocationHandler.java:84)
    at org.codehaus.xfire.service.binding.ServiceInvocationHandler.execute(ServiceInvocationHandler.java:132)
    at org.codehaus.xfire.service.binding.ServiceInvocationHandler.invoke(ServiceInvocationHandler.java:107)
    at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
    at org.codehaus.xfire.transport.DefaultEndpoint.onReceive(DefaultEndpoint.java:64)
    at org.codehaus.xfire.transport.AbstractChannel.receive(AbstractChannel.java:38)
    at org.codehaus.xfire.transport.http.XFireServletController.invoke(XFireServletController.java:301)
    at org.codehaus.xfire.transport.http.XFireServletController.doService(XFireServletController.java:130)
    at org.codehaus.xfire.spring.remoting.XFireServletControllerAdapter.handleRequest(XFireServletControllerAdapter.java:67)
    at org.codehaus.enunciate.modules.xfire.EnunciatedXFireExporter.handleRequest(EnunciatedXFireExporter.java:54)
    at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:809)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:511)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.codehaus.enunciate.modules.spring_app.HTTPRequestContextFilter.doFilter(HTTPRequestContextFilter.java:36)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)

    I have made mapping of table into hibernate.xml’ configuration, still some error comes to end.
    GINP.
    please notify me of follow-up comments immediately.

    Reply
  2. Paras

    Hi Jay,
    Sorry for the late reply. Have you found the solution for this yet? Looks like some mapping mistake you are making.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *