C# 延迟加载返回相等的记录
我有以下实体:C# 延迟加载返回相等的记录,c#,nhibernate,lazy-loading,C#,Nhibernate,Lazy Loading,我有以下实体: public class Document : PersistentObjectBase { public virtual long? FolderId { get; set; } } 该方法返回包含子文件夹和文档的文件夹,但可以在列表中多次找到该文件夹或文档。 例如,我只有30个文件夹,但我得到了大约180个,其中一个文件夹超过1次。 对不起,我的英语很差…很像你的Joinely类型。使用另一种类型(例如Select)
public class Document : PersistentObjectBase
{
public virtual long? FolderId
{
get;
set;
}
}
该方法返回包含子文件夹和文档的文件夹,但可以在列表中多次找到该文件夹或文档。
例如,我只有30个文件夹,但我得到了大约180个,其中一个文件夹超过1次。
对不起,我的英语很差…很像你的Joinely类型。使用另一种类型(例如Select),LeftOuterJoin为每个1:n关系返回n行。只需使用Select,不要使用任何类型的联接
<id name="Id" column="ID">
<generator class="identity" />
</id>
<version column="VERSION" name="Version" type="Int64" unsaved-value="undefined" />
<property name="FolderId" column="FOLDER" />
public class DocumentFolder : PersistentObjectBase
{
public virtual long? ParentFolderId
{
get;
set;
}
#region [Lazy subfolders]
protected virtual IList<DocumentFolder> NSubFolders
{
get;
set;
}
public virtual IList<DocumentFolder> SubFolders
{
get
{
return GetObjectWithInitializationCheck(NSubFolders);
}
set
{
NSubFolders = value;
}
}
#endregion
#region [Lazy Documents]
protected virtual IList<Document> NDocuments
{
get;
set;
}
public virtual IList<Document> Documents
{
get
{
return GetObjectWithInitializationCheck(NDocuments);
}
set
{
NDocuments = value;
}
}
#endregion
}
protected static T GetObjectWithInitializationCheck<T>(T propertyValue) where T : class
{
if (ObjectInitialized(propertyValue))
{
return propertyValue;
}
return null;
}
<id name="Id" column="ID">
<generator class="identity" />
</id>
<version column="VERSION" name="Version" type="Int64" unsaved-value="undefined" />
<property name="ParentFolderId" column="PARENT_FOLDER_ID" />
<bag name="NSubFolders" lazy="true" cascade="delete">
<key column="PARENT_FOLDER_ID" />
<one-to-many class="DocumentFolder" />
</bag>
<bag name="NDocuments" lazy="true">
<key column="FOLDER" />
<one-to-many class="Document" />
</bag>
criteria.SetResultTransformer(new DistinctRootEntityResultTransformer());
criteria.CreateCriteria("NSubFolders", "subFolders", JoinType.LeftOuterJoin);
criteria.CreateCriteria("NDocuments", "documents", JoinType.LeftOuterJoin);