Skip to content

Instantly share code, notes, and snippets.

@Meligy
Created March 19, 2011 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Meligy/877532 to your computer and use it in GitHub Desktop.
Save Meligy/877532 to your computer and use it in GitHub Desktop.
How to eager load Child Collections (Ex: Blog.Post.Comments) Directly Using QueryOver In NHibernate 3.1
session.QueryOver<Blog>()
.Fetch(b => b.Posts).Eager // If you don't add this, the following will not work
.Fetch(b => b.Posts.First().Comments).Eager // The trick here is using ".First()" after the collection
.List();
@Meligy
Copy link
Author

Meligy commented Mar 19, 2011

Result Query (SQL CE 4.0):

SELECT
    this_.Id as Id0_2_,
    this_.Title as Title0_2_,
    posts2_.Blog_id as Blog3_4_,
    posts2_.Id as Id4_,
    posts2_.Id as Id2_0_,
    posts2_.Title as Title2_0_,
    comments3_.Post_id as Post3_5_,
    comments3_.Id as Id5_,
    comments3_.Id as Id1_1_,
    comments3_.Title as Title1_1_
FROM
    "Blog" this_
left outer join
    "Post" posts2_
        on this_.Id=posts2_.Blog_id
left outer join
    "Comment" comments3_
        on posts2_.Id=comments3_.Post_id

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment