I have entities:
public class Document : PersistentObjectBase
{
public virtual long? FolderId
{
get;
set;
}
}
<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
}
where:
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>
While building criteria, I do:
criteria.SetResultTransformer(new DistinctRootEntityResultTransformer());
criteria.CreateCriteria("NSubFolders", "subFolders", JoinType.LeftOuterJoin);
criteria.CreateCriteria("NDocuments", "documents", JoinType.LeftOuterJoin);
The method returns folders with subfolders and documents, but it is possible to find the folder or documents multiple times in the lists. For example, I have only 30 folders but I get about 180 where there is one folder mare than 1 times. Sorry for my poor English...