【问题标题】:HQL - join with only one subclassesHQL - 只加入一个子类
【发布时间】:2012-10-11 17:33:21
【问题描述】:

我的模型(InheritanceType.JOINED):

class Parent{...} 
class Child1 extends Parent{...}   
class Child2 extends Parent{...}
class Child3 extends Parent{...}
class Child4 extends Parent{...}
class Agg{
  List<Parent> l;
}

Agg 通过连接表与父级连接。父级没有 Agg 对象。

我正在对 Child2 进行一些过滤,即: " 从 Child2 ch2 WHERE ch2.field1 =... ch2.field2 =... 等"

我现在如何将 Child2 与 Agg 连接,而不导致与所有子类表连接。 我只想加入 Child2 表(不加入 Child2、Child3、Child4)

我试图使用“类”属性(即 ch2.class=...)结果是正确的,但生成的查询包含对每个子类的连接;/

HQL "From Agg a Join a.l" 也加入所有子类(即使是 ch2.class)

谢谢!

【问题讨论】:

  • 也许你在 Agg 类的 List 上有一个“急切”的加载,导致了这个。当您使用 Parent 类进行 JOIN 时,您将有这种行为(因此,我从不使用 Hibernate 的继承)。您能否提供更多详细信息,例如所有类的映射?

标签: hibernate inheritance hql


【解决方案1】:

您可以使用交叉连接来解决此问题,因为无论如何 DBMS 都会将其优化为内部连接。像这样SELECT ch2, agg FROM Child2 ch2, Agg agg WHERE ch2.joinField = agg.id AND ch2.field1 =... AND ch2.field2 =...

【讨论】:

    猜你喜欢
    • 2011-09-14
    • 2013-09-10
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 2016-06-24
    相关资源
    最近更新 更多