Question

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.

Was it helpful?

Solution

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);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top