【问题标题】:NHibernate Eager Fetching Over Multiple Levels with StatelessSessionNHibernate Eager fetching over multiple level with StatelessSession
【发布时间】:2011-01-20 05:34:36
【问题描述】:

我引用了以下线程,该线程适用于 Session,但不适用于 StatelessSession。 NHibernate Eager Fetching Over Multiple Levels

我遇到的问题类似于上面的线程。我回来了 4xApplications 和 4xRoles。我应该找回 1xApplication 和 4xRoles。

条件查询:

return StatelessSession.CreateCriteria(typeof(Application))
            .SetResultTransformer(Transformers.DistinctRootEntity)
            .SetFetchMode("Roles", FetchMode.Join)
            .List<Application>();

我的实体和映射:

public class Application : IComparable<Application>
{
    public virtual int Id { get;  set; }
    public virtual string InternalName { get; set; }
    public virtual ICollection<Role> Roles { get; set; }

    #region IComparable<Application> Members

    public virtual int CompareTo(Application other)
    {
        return Id.CompareTo(other.Id);
    }

    #endregion

}

public class Role : IComparable<Role>
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual Application Application { get; set; }

    public virtual int CompareTo(Role other)
    {
        return Id.CompareTo(other.Id);
    }

}

public class ApplicationMapping : ClassMap<Application>
{
    public ApplicationMapping()
    {
        Table("Application");
        Id(x => x.Id, "ID").GeneratedBy.Assigned(); ; //.Column("ID");
        Map(x => x.InternalName);
        HasMany(x => x.Roles).Inverse().Cascade.All().AsSet().Not.LazyLoad();
    }
}

public class RoleMapping : ClassMap<Role>
{
    public RoleMapping()
    {

        Table("Roles");
        Id(x => x.Id, "ID").GeneratedBy.Assigned(); 
        References(x => x.Application, "ApplicationID").Not.LazyLoad();
        Map(x => x.Name);
        HasMany(x => x.RightsUsers).Table("UserRoleXRef").Inverse().Cascade.All().AsSet();
    }   
}

hbm.xml

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property"     auto-import="true" default-cascade="none" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" name="Quad.App.Test.DataAccess.NHibernate.Scaffolding.Entities.Application, Quad.App.Test, Version=1.4.0.742, Culture=neutral, PublicKeyToken=null" table="Application">
<id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
  <column name="ID" />
  <generator class="assigned" />
</id>
<property name="InternalName" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
  <column name="InternalName" />
</property>
<set cascade="all" inverse="true" lazy="false" name="Roles">
  <key>
    <column name="ApplicationID" />
  </key>
  <one-to-many class="Quad.App.Test.DataAccess.NHibernate.Scaffolding.Entities.Role, Quad.App.Test, Version=1.4.0.742, Culture=neutral, PublicKeyToken=null" />
</set>
   </class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property"     auto-import="true" default-cascade="none" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" name="Quad.App.Test.DataAccess.NHibernate.Scaffolding.Entities.Role, Quad.App.Test, Version=1.4.0.742, Culture=neutral, PublicKeyToken=null" table="Roles">
<id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
  <column name="ID" />
  <generator class="assigned" />
</id>
<property name="Name" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
  <column name="Name" />
</property>
<many-to-one class="Quad.App.Test.DataAccess.NHibernate.Scaffolding.Entities.Application, Quad.App.Test, Version=1.4.0.742, Culture=neutral, PublicKeyToken=null" lazy="false" name="Application">
  <column name="ApplicationID" />
</many-to-one>
  </class>
</hibernate-mapping>

SQL:

SELECT this_.ID as ID1_1_, 
this_.InternalName as Internal2_1_1_, 
roles2_.ApplicationID as Applicat3_3_, 
roles2_.ID as ID3_, 
roles2_.ID as ID2_0_, 
roles2_.Name as Name2_0_, 
roles2_.ApplicationID as Applicat3_2_0_ 
FROM dbo.Application this_ left outer join dbo.Roles roles2_ on       
this_.ID=roles2_.ApplicationID

【问题讨论】:

  • 很奇怪。您是否意识到您已经将角色映射为非懒惰?我想知道 FetchMode 是否不喜欢被告知两次?如果删除 SetFetchMode,它如何非懒惰地获取角色?外连接还是选择?
  • 然后我得到一个异常,你不能延迟加载角色列表。它只是从应用程序表中选择。 SELECT this_.ID as ID1_0_, this_.InternalName as Internal2_1_0_ FROM dbo.Application this_ {"Initializing[Quad.App.Test.DataAccess.NHibernate.Scaffolding.Entities.Application#50]-未能延迟初始化角色集合:Quad .App.Test.DataAccess.NHibernate.Scaffolding.Entities.Application.Roles,没有会话或会话被关闭"}

标签: nhibernate


【解决方案1】:

我怀疑这是因为生成的 sql 查询在角色加入时会产生重复的应用程序。

避免这种情况的最简单方法是使用 ISet(HashedSet 实现)而不是 ICollection(在 Iesi.Collections.dll 中定义),这些设计使您不会在列表中得到重复项。

【讨论】:

    猜你喜欢
    • 2012-02-07
    • 2015-04-06
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    相关资源
    最近更新 更多