73

我正在尝试按照 hql 查询执行

SELECT count(*) 
  FROM BillDetails as bd
 WHERE bd.billProductSet.product.id = 1002
   AND bd.client.id                 = 1

但它显示

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)
4

2 回答 2

155

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

Product是this元素的一个属性Collection

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

SELECT count(*) 
  FROM BillDetails        bd 
  JOIN bd.billProductSet  bps 
 WHERE bd.client.id       = 1
   AND bps.product.id     = 1002
于 2014-07-15T06:16:52.670 回答
1

因为 billProduct 是一对多映射,并且一个 BillDetails 实体中有许多 billProduct 实体,所以您无法在查询中取消引用它。您必须将 BillDetails 模型加入 billProduct 并使用 where cluase 过滤结果。

于 2016-11-19T11:13:03.960 回答