Tuesday, November 6, 2012

NHibernate, bug in deep load of bags


Some days ago I write about performance difference between bag and set mapping of collections. But not all is so sunny with the bags. Let's look on the following example.
Model

We have blogs that contains posts, and posts that contain comments.  And we want to map Blog.Posts and Post.Comments associations as <bag>.
Now let's create new blog. Then add four posts. Then add one comment to second post, two comments to third post and three comments to forth post.  In total we added 7 comments to 4 posts.
But when we try to fetch whole graph of blog with its posts and with their comments in one single query we get strange results.
session.Query<Blog>()
   .Where(b => b.Id == expected.Id)
   .FetchMany(b => b.Posts)
   .ThenFetch(p => p.Comments)
   .ToList().First();


Our blog have 7 posts instead of 4. SQL query returned 7 joined rows from the DB (as expected) but it seems that NHibernate did not transformed properly received results.
If we map Blog.Posts as <set>  it works fine. So that issue appears only with bags.
I have opened a bug on NHibernate's JIRA issue tracker. There are tests that I have provided.

No comments:

Post a Comment