org.hibernate.QueryException:取消引用集合的非法尝试
我正在尝试执行以下hql查询org.hibernate.QueryException:取消引用集合的非法尝试,hibernate,hql,jpa-2.0,dereference,jpa-2.1,Hibernate,Hql,Jpa 2.0,Dereference,Jpa 2.1,我正在尝试执行以下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.billPro
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)
billProductSet
是一个集合
。
因此,它没有名为product
的属性
产品
是此集合
元素的属性
您可以通过加入集合而不是取消引用来解决问题:
选择计数(*)
来自BillDetails bd
加入bd.billProductSet bps
其中bd.client.id=1
和bps.product.id=1002
因为billProduct是一对多映射,并且一个BillDetails实体中有多个billProduct实体,所以您不能在查询中取消引用它。您必须将BillDetails模型连接到billProduct,并使用where CLUSE筛选结果。如果billProductSet为@JoinTable,则在我的测试中不起作用,而且关系是ManyToMany。@Stony它确实与@JoinTable
和@ManyToMany
一起工作。我现在正在运行它。仅供参考:在我的例子中,我已经在做集合的联接,但是没有给它一个别名,它将无法工作。谢谢您可以添加类的定义吗?它们是否包含关系定义?