org.hibernate.QueryException:非法尝试取消引用集合

xrc*_*wrn 56 hibernate hql dereference jpa-2.0 jpa-2.1

我正在尝试跟随hql查询执行

SELECT count(*) 
  FROM BillDetails as bd
 WHERE bd.billProductSet.product.id = 1002
   AND bd.client.id                 = 1
Run Code Online (Sandbox Code Playgroud)

但它正在显示

org.hibernate.QueryException: illegal attempt to dereference collection 
[billdetail0_.bill_no.billProductSet] with element property reference [product] 
[select count(*) from iland.hbm.BillDetails as bd where bd.billProductSet.product.id=1001 and bd.client.id=1]
    at org.hibernate.hql.ast.tree.DotNode$1.buildIllegalCollectionDereferenceException(DotNode.java:68)
    at org.hibernate.hql.ast.tree.DotNode.checkLhsIsNotCollection(DotNode.java:558)
Run Code Online (Sandbox Code Playgroud)

kos*_*tja 121

billProductSet是一个Collection.因此,它没有名为的属性product.

Product是一个属性的元素的此Collection.

您可以通过加入集合而不是取消引用它来解决问题:

SELECT count(*) 
  FROM BillDetails        bd 
  JOIN bd.billProductSet  bps 
 WHERE bd.client.id       = 1
   AND bps.product.id     = 1002
Run Code Online (Sandbox Code Playgroud)

  • @Stony 它**确实**与`@JoinTable` 和`@ManyToMany` 一起使用。我现在正在运行。 (3认同)
  • 仅供参考:在我的例子中,我已经在进行集合的连接,但如果不给它一个别名,它将无法工作。谢谢! (2认同)