NHibernate QueryOver with Fetch resulting multiple sql queries and db hits
I'm trying to select an entity and fetch a related list:
Session.QueryOver<UserRole>()
.Fetch(x => x.UsersInRole).Eager
.List();
Which results in a lot of database hits. The first one is something like:
SELECT ... FROM UserRoles left outer join UsersInRoles on ...
And hundreds more seperate queries which looks something like:
SELECT ... FROM UsersInRoles left outer join UserRoles on ... WHERE UserRoles.UserId=?
The mapping is as following:
public class UserRoleMap : ClassMap<UserRole>
{
public UserRoleMap()
{
Id(x => x.Id);
Map(x => x.RoleName);
HasManyToMany(x => x.UsersInRole)
.Inverse()
.LazyLoad()
.Table("UsersInRoles");
}
}
I would say, that this behaviour is what we should expect. Let's have a scenario, in which we have in the system 2 users and 2 roles
User1 - Role1 // has only Role1
User2 - Role1 // now we see that Role2 has more then User1
User2 - Role2
Let's say, that the first query, will retrieve only User1 and its many-to-many relation Role1. What we have in the ISession
at the moment is only User1, so the set of Users for Role1 is incomplete (we cannot reuse objects loaded into ISession at the moment). But how should one know where we are? That all data loaded for Role1
is or is not in the session?
New query, loading the data for Role1
must be issued. And this way, we can at the end have dosens of these queries...
What I see as the best solution (I am using it in almost all scenarios) is the batch-size
setting: 19.1.5. Using batch fetching
HasManyToMany(x => x.UsersInRole)
...
.BatchSize(25)
Mark all your collection maps with .BatchSize(25)
and do that even for the Class map as well. That will cause more then 1 SQL Script, but at the end not more then 1 + (2-4) dependent on the batch-size and page size.