【问题标题】:How to implicitly join across collections in Hibernate?如何在 Hibernate 中隐式连接集合?
【发布时间】:2011-04-20 18:16:38
【问题描述】:

对象结构如下-

Entity A
  - Collection <B>

Entity B
  - Collection <C>

Entity C
  -Collection <D>

Entity D
  CompositePrimaryKey

Class CompositePrimaryKey
  String id;

在 HQL 中,查询类似于 from A a where a.B.C.D.CompositePrimaryKey.id = 'input';

我收到以下异常-

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

基本上这是跨集合的隐式连接。 这样做的正确方法是什么?

我想通过集合获取所有数据 - 基于 id 值

谢谢。

【问题讨论】:

    标签: hibernate orm


    【解决方案1】:

    据我所知,HQL 无法取消引用这个复杂的案例。您需要将子集合的映射指定为 fetch="join" 并手动编写大 HQL 连接:

    from A_table a, B_table b, C_table c, D_table d
      where a.b_id = b.id and b.c_id = c.id and c.d_id = d.id and d.id = 'input'
    

    一个好的解决方案也是为实体A 定义filter(过滤器对表列进行操作):

    <class name="A" ...>
        ...
        <filter name="id_filter" condition="D_table.id = :id" />
    </class>
    

    【讨论】:

    • 我使用了过滤器逻辑,它可以很好地满足我的要求,谢谢。
    【解决方案2】:

    我收到 (...) 异常 org.hibernate.QueryException: 非法尝试取消引用集合

    路径表达式a.B.C.D 是非法的,因为 B(以及 C 和 D)是一个集合。

    基本上这是跨集合的隐式连接。这样做的正确方法是什么?

    使用显式连接:

    from A a join a.B b join b.C c join c.D d where d.pk.id = :id
    

    【讨论】:

    • 我比旧的 where 子句连接语法更喜欢这种连接语法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 2012-09-22
    • 1970-01-01
    相关资源
    最近更新 更多