【问题标题】:org.hibernate.QueryException: illegal attempt to dereference collectionorg.hibernate.QueryException:非法尝试取消引用集合
【发布时间】:2014-09-05 05:00:53
【问题描述】:

我正在尝试按照 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)

【问题讨论】:

  • 你能添加你的类的定义吗?它们是否包含关系定义?

标签: hibernate hql jpa-2.0 dereference jpa-2.1


【解决方案1】:

billProductSetCollection。 因此,它没有名为product 的属性。

Product 是这个Collection元素的属性

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

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

【讨论】:

  • 在我的测试中不起作用,如果billProductSet是@JoinTable,并且关系是ManyToMany。
  • @Stony 它确实适用于@JoinTable@ManyToMany。我现在正在运行。
  • 仅供参考:在我的情况下,我已经在进行集合的连接,但如果不给它一个别名,它将无法工作。谢谢!
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2015-11-27
    • 1970-01-01
    • 2020-04-17
    • 2011-09-18
    • 2012-10-17
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多