Search code examples
nhibernatefluent-nhibernatelinq-to-nhibernatenhibernate-criteria

NHibernate joins


public class Parent
{
    public virtual long Id { get; set; }
    public virtual string Name { get; set; }
    public virtual List<Child> Children { get; set; }
}

public class Child
{
    public virtual long Id { get; set; }
    public virtual string Description { get; set; }
    public virtual Parent Parent { get; set; }
}

In NHibernate, how can I get a list of children with their parent name using only one call to db. I tried:

CurrentSession.QueryOver < Child>().JoinQueryOver(x=>x.Parent).Fetch(x => x.Parent).Eager.List< Child>().ToList();

but it didn't work.

The mappings are done using FluentNHibernate.


Solution

  • so close:

    CurrentSession.QueryOver<Child>()
      .Fetch(x => x.Parent).Eager
      .List<Child>();
    

    should work (you don't need the extra call to .ToList, or the join on to Parent)