日期:2014-05-20  浏览次数:20858 次

关于Hibernate HQL IN()操作符问题!
各位好,今天用了一下IN()操作符,但是报错,请大家帮忙看一下,我用的是Hibernate 3以上版本。

代码摘要:

String itemsql = "from Item i in(i.bids) b where b.amountCurrency >200 ";
List itemList = secondSession.createQuery(itemsql).list();

错误摘要:

Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: unexpected token: in near line 1, column 28 [select i from hello.Item i in(i.bids) b where b.amountCurrency >200 ]
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)

------解决方案--------------------
写错了

给你例句

Java code
from DomesticCat cat where cat.name in ( 'Foo', 'Bar', 'Baz' )

------解决方案--------------------
这种语法明显是错的,可能是印刷错误, from Item i in(i.bids) b ,这个 b 是什么,可以放在这里吗? 

HQL 和 SQL及其类似,你SQL能这样写吗? 这个 b 应该是前面那句查询出来的子集的别名。

( select ..... from Item i in(i.bids) ) b where b.amountCurrency >200

看看加上红色部分的括号,把 b 当作结果集试试。
------解决方案--------------------
第一次见这种写法,一直都以为像2楼那样写的
------解决方案--------------------
IN前面应该有个逗号。
SQL code
FROM Item i, IN(i.bids) b WHERE b.amountCurrency > 200