1

我有两个用 NHibernate 映射的类:类 Application 引用类 Store 和属性 StoreId。应用程序用户有一个身份 ID,而类 Store 有一个分配的 ID,但我认为在这种情况下这并不重要。

应用用户映射:

<many-to-one name="Store" column="StoreId" class="Store" />

商店映射:

<many-to-one name="ApplicationUser" column="Id" class="ApplicationUser" 
    property-ref="Store" insert="false" update="false" 
    fetch="join" outer-join="true" />

当我加载所有 Store 时,会按预期为 ApplicationUser 生成一个左外连接,但是在构建对象图时 NHibernate 决定SELECT ... FROM ApplicationUser WHERE StoreId = ?为每个不引用 ApplicationUser 的 Store 做一个额外的操作。

这是巨大的矫枉过正,完全没有必要,因为它应该已经知道那些 ApplicationUser 不存在。

任何人都知道如何阻止 NHibernate 生成这些额外的查询?

编辑:

类是非常基础的,像这样:

public class Store
{
    public virtual int Id { get; set; }
    // ...
    public virtual ApplicationUser ApplicationUser { get; set; }
}

public class ApplicationUser
{
    public virtual int Id { get; set; }
    // ...
    public virtual Store Store { get; set; }
}
4

1 回答 1

0

我认为这里发生的事情是 nHibernate 正在尝试加载两个集合,因为您已将每个集合指定为“多对一”...我认为除了修改映射之外您无能为力...

所以 a -> xb 和 b -> xa,我的理解是 nHibernate 会查询这两种关系......对我来说很有意义。

如果不需要集合,则不能将其包含在查询中并依赖延迟加载。

于 2011-05-05T21:35:39.943 回答