gpt4 book ai didi

NHibernate Eager 使用 StatelessSession 进行多个级别的获取

转载 作者:行者123 更新时间:2023-12-03 11:33:12 25 4
gpt4 key购买 nike

我引用了以下线程,该线程适用于 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>

查询语句:
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

最佳答案

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

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

关于NHibernate Eager 使用 StatelessSession 进行多个级别的获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2321033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com