1
我遇到了我的多对多XML映射问题。最近我使用NHibernate Profiler并发现了这个Select N + 1问题。我似乎无法找到一个可行的解决方案,并且不知道我的映射是好还是与其查询有关。用于Select N + 1的NHibernate QueryOver解决方案
场景:我有一个配置文件可以有多个预定义的标签。标签可以有父母和/或孩子。这些标签可以在许多配置文件中使用。我会告诉你我的相关类和映射。
Account.cs
public class Account
{
public int Id { get; set; }
public Profile Profile { get; set; }
}
Profile.cs
public class Profile
{
public int Id { get; set; }
public IList<ProfileTag> Tags { get; set; }
}
Profile.hbm.xml
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Agrolink.Application.Models" assembly="Agrolink.Application">
<class name="Agrolink.Application.Models.Profile" lazy="false" table="Profiles" >
<id name="Id" column="Id" >
<generator class="identity" />
</id>
<bag name="Tags" table="ProfileTags" cascade="all-delete-orphan" inverse="true">
<key column="IdProfile" not-null="true"/>
<one-to-many class="Agrolink.Application.Models.ProfileTag" />
</bag>
</class>
</hibernate-mapping>
ProfileTag.cs
public class ProfileTag
{
public int Id { get; set; }
public Profile Profile { get; set; }
public Tag Tag { get; set; }
}
ProfileTag.hbm.xml
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Agrolink.Application.Models" assembly="Agrolink.Application">
<class name="Agrolink.Application.Models.ProfileTag" lazy="false" table="ProfileTags" >
<id name="Id" column="Id" >
<generator class="identity" />
</id>
<many-to-one name="Profile" class="Agrolink.Application.Models.Profile" column="IdProfile" cascade="save-update" />
<many-to-one name="Tag" class="Agrolink.Application.Models.Tag" column="IdTag" cascade="none" />
</class>
</hibernate-mapping>
Tag.cs
public class Tag
{
public int Id { get; set; }
public Tag Parent { get; set; }
public IList<Tag> Children { get; set; }
}
Tag.hbm.xml
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Agrolink.Application.Models" assembly="Agrolink.Application">
<class name="Agrolink.Application.Models.Tag" lazy="false" table="Tags">
<id name="Id" column="Id" >
<generator class="identity" />
</id>
<property name="Name" column="Name" />
<property name="Type" type="Agrolink.Application.Models.TagType, Agrolink.Application" column="IdType" />
<many-to-one name="Parent" class="Agrolink.Application.Models.Tag" column="IdParent" cascade="none" />
<bag name="Children" table="Tags" cascade="all" inverse="true">
<key column="IdParent" not-null="true"/>
<one-to-many class="Agrolink.Application.Models.Tag" />
</bag>
</class>
</hibernate-mapping>
当我QueryOver帐户我看到hes选择单独的所有标签。我怎样才能防止这一点?我尝试使用JoinAlias,但它没有任何区别。
我的地图可以和父母/孩子一起吗?
所有帮助表示赞赏! Thx
是的,我最喜欢的解决方案。有关该功能的更多信息,我在[本答案](/ a/36070727/1178314)中写了详细的解释。 (你可以接受你自己的答案。) –