我引用了以下线程,该线程适用于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
答案 0 :(得分:0)
我怀疑这是因为生成的sql查询会在角色加入时生成重复的应用程序。
避免这种情况的最简单方法是使用ISet(HashedSet&lt;&gt;实现)而不是ICollection(在Iesi.Collections.dll中定义),这些设计使您不会在列表中获得重复项。