【问题标题】:How to prevent nhibernate from creating left outer joins for subclasses如何防止 nhibernate 为子类创建左外连接
【发布时间】:2011-02-04 06:09:41
【问题描述】:

当查询一个基类时,NHibernate 将为每个基类的子类创建一个左外连接。即使查询仅针对基础中的属性并且在这些属性上设置了投影,也会发生这种情况。

例子:

SELECT this_.Id as y0_, this_.Name as y1_ FROM AnimalBase this_ 
    left outer join Dog this_1_ on this_.Id=this_1_.AnimalBaseFK 
    left outer join Cat this_2_ on this_.Id=this_2_.AnimalBaseFK 
WHERE lower(this_.Name) like @p0;@p0 = '%rover%'

我的问题是我应该如何改变这个默认行为?

谢谢

编辑:

看这里:

How to perform a non-polymorphic HQL query in Hibernate?

【问题讨论】:

    标签: nhibernate inheritance criteria


    【解决方案1】:

    这是因为 NHibernate 默认使用多态查询。 我的意思是,如果你这样做:

    select * from Animal
    

    NHibernate 将尝试检索所有动物。所以,如果你有一个继承自Animal 的Dog 类,这意味着狗是动物。 (这是逻辑,因为继承描述了IS-A 关系)。

    您可以禁用此行为。 更多信息:Loading a base class through nhibernate incorrectly uses mappings from derived classes

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多