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:
- Why is this?
- How do I prevent this?
Thanks!