关于Hibernate HQL IN()操作符问题!

sixbirds01 2009-11-12 05:08:00
各位好,今天用了一下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)
...全文
3228 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
超越自己 2011-11-02
  • 打赏
  • 举报
回复
这书,一个逗号要人命,呵呵,看你们的讨论,我也有收获了,正找hql 是否能in操作呢。呵呵
ThirstyCrow 2009-11-13
  • 打赏
  • 举报
回复
IN前面应该有个逗号。
FROM Item i, IN(i.bids) b WHERE b.amountCurrency > 200

sixbirds01 2009-11-13
  • 打赏
  • 举报
回复
原书例句:

select i from Item i in(i.bids) b where i.description like '%Foo' and b.amount>100

Hibernate实战(第二版),第478页,页首部。

有这本数的朋友可以看看。
sixbirds01 2009-11-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 youjianbo_han_87 的回复:]
这种语法明显是错的,可能是印刷错误, 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 当作结果集试试。
[/Quote]

可能是书写错误,但是我能大概理解其中的意思,暂时没有找到答案...
gjsong 2009-11-13
  • 打赏
  • 举报
回复
第一次见这种写法,一直都以为像2楼那样写的
youjianbo_han_87 2009-11-13
  • 打赏
  • 举报
回复
这种语法明显是错的,可能是印刷错误, 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 当作结果集试试。
sixbirds01 2009-11-13
  • 打赏
  • 举报
回复
[Quote=引用楼主 sixbirds01 的回复:]
各位好,今天用了一下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)
[/Quote]


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

这个例子我按书上改的,是使用From子句实现内连接的一种方式。
大概意思是出出价金额大于200的所有已出价的商品和出价的信息。

sixbirds01 2009-11-13
  • 打赏
  • 举报
回复
书中少写了一个逗号:)
sixbirds01 2009-11-13
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 thirstycrow 的回复:]
IN前面应该有个逗号。
SQL codeFROM Item i,IN(i.bids) bWHERE b.amountCurrency>200

[/Quote]

OK! 测试后通过。

Hibernate 实战(第二版),书中摘要:

HQL和JPA QL提供另外一种语法,用于在FROM子句中连接集合,并给它分配一个别名。
这个IN()操作符在更早版本的EJB QL中就出现了。
它的语义与普通集合连接的一样。
from Item i,in(i.bids) b 生成了与上面的示例中用from Item i join i.bids b一样的内部连接。

谢谢各位的支持!
zl3450341 2009-11-12
  • 打赏
  • 举报
回复
写错了

给你例句

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

67,516

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧