문제

Given the following entity:

public class Comment
{
    public int Id { get; set; }
    public IList<Comment> ChildComments { get; set; }
}

I basically want to load the full tree, ie the comment with its children plus their children and so on using QueryOver for a given Id. Unfortunately i dont really know where to even begin.

Setting the mappings to eager load the collection isnt an option for me either.

Is there a way to do this easily or do i need to use hql in this case?

Thanks.

도움이 되었습니까?

해결책

Well, I think you need future queries and DistinctRootEntityResultTransformer

I can show you how I do it with Criteria and also recommend you to do the same. In our project everything is LINQ but this one had to be ugly and with magic strings. This is Group object with Many Members and Many SubGroups. After executing the query I get the root (the root is the element which parent is null) which hold LOADED objects in it. For your case you should add a restriction by CommendId Also, each fetch have to be done in a separate future query. Good luck

public static GroupDto GetGroupHierarchy(this ISession session)
    {
        session
            .CreateCriteria<GroupDto>()
            .Add(Expression.Eq("IsActive", true))
            .SetFetchMode("Subgroups", NHibernate.FetchMode.Eager)
                .SetResultTransformer(new DistinctRootEntityResultTransformer())
                .Future<GroupDto>();

        var groups = session
               .CreateCriteria<GroupDto>()
               .Add(Expression.Eq("IsActive", true))
                   .SetFetchMode("Members", FetchMode.Eager)
                   .SetResultTransformer(new DistinctRootEntityResultTransformer())
                   .Future<GroupDto>();

        return groups.Single(g => g.Parent == null);
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top