开发者

Lazy loading returns equal records

开发者 https://www.devze.com 2023-01-22 19:25 出处:网络
I have entities: public class Document : PersistentObjectBase { public virtual long? FolderId { get; set; } }

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...


It's most likeley your JoinType. Use another Type (e.g. Select), LeftOuterJoin returns n rows for every 1:n relation.

0

精彩评论

暂无评论...
验证码 换一张
取 消