0

I'm creating a HQL query that uses eager fetching for some collections and properties. This is the query:

var result = Session.CreateQuery("from Match m left join fetch m.User u left join fetch u.ProfileItems pi where m.User.Id = :p1") .SetParameter("p1", user.Id) .List<Match>().ToList(); 

This works well. Using NHProfiler, I can see that it produces the following SQL query:

select (...) from `Match` match0_ left outer join `User` user1_ on match0_.UserId = user1_.Id left outer join `ProfileItem` profileite2_ on user1_.Id = profileite2_.User_id where match0_.UserId =? p0 

Now, I can iterate over the result of Match instances or even User instances like this:

foreach (User u in result.Select(m => m.User)) { // .. do something } 

... and it will not hit the database again.

However, the User object has a property that is not mapped to the database, IsOnline. This does some calculation using the current date. As soon as I use this property in an expression, the database will be hit. For example:

int i = result.Count(m => m.MatchingUser.IsOnline); 

... will hit the database for every user, in my case 10 times, using this query:

SELECT * FROM `User` user0_ WHERE user0_.Id =? p0 

I have two main questions:

  1. Why is this?
  2. How do I prevent this?

Thanks!

1
  • How does your mapping look like ? Commented Feb 9, 2011 at 14:26

1 Answer 1

1

Unless it's a copy&paste error, you are using a different property (MatchingUser) from the one you fetched (User)

Sign up to request clarification or add additional context in comments.

1 Comment

Seriously, you have no idea how stupid this makes me feel :-) This took like 2 hours of my time yesterday. Thanks for reading my post in detail.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.