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.

01Caused 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 ]
02    at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:31)
03    at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:24)
04    at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
05    at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:235)
06    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
07    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
08    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
09    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
10    at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
11    at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
12    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1113)
13    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
14    at com.test.FruitDAOServiceImpl.fruitCount(FruitDAOServiceImpl.java:682)
15    at jrockit.reflect.VirtualNativeMethodInvoker.invoke(Ljava.lang.Object;[Ljava.lang.Object;)Ljava.lang.Object;(Unknown Source)
16    at java.lang.reflect.Method.invoke(Ljava.lang.Object;[Ljava.lang.Object;I)Ljava.lang.Object;(Unknown Source)
17    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
18    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:172)
19    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:139)
20    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

1public Long fruitCount(List<Long> fruitIdList){
2    ........
3    Query fruitQuery = getSession().createQuery("select count(*)  from com.test.Fruits as fruit where fruit.fruitId in (:fruitIdList) order by fruit.fruitName desc ");
4    fruitQuery.setParameterList("fruitIdList", fruitIdList);
5    List<Long> fruitCountList = fruitQuery.list();
6    ..........
7}

I did this

01public Long fruitCount(List<Long> fruitIdList){
02    ........
03    StringBuffer queryString =newStringBuffer("select count(*)  from com.test.Fruits as fruit ");
04  
05    if(fruitIdList !=null&& !fruitIdList.isEmpty()){
06        queryString.append(" where fruit.fruitId in (:fruitIdList) ");
07    }
08  
09    queryString.append(" order by fruit.fruitName desc ");
10  
11    Query fruitQuery = getSession().createQuery(queryString.toString());
12    if(fruitIdList !=null&& !fruitIdList.isEmpty()){
13        fruitQuery.setParameterList("fruitIdList", fruitIdList);
14    }
15  
16    List<Long> fruitCountList = fruitQuery.list();
17    ..........
18}

In short, append the collection parameter in the query and the query parameter only if the collection is non-empty.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值