I have following mapping
public class FilmMap : ClassMap<Film>
{
public FilmMap()
{
Id(x => x.FilmId, "film_id");
Map(x => x.Description);
base.HasMany<FilmActor>(x => x.FilmActor).BatchSize(100);
}
}
public class FilmActorMap : ClassMap<FilmActor>
{
public FilmActorMap()
{
Table("film_actor");
CompositeId()
//.KeyReference(x => x.Actor, "actor_id")
.KeyProperty(x => x.ActorId, "actor_id")
.KeyProperty(x => x.FilmId, "film_id");
Map(x => x.LastUpdate, "last_update");
References<Actor>(x => x.Actor, "actor_id"); //.Fetch.Join();
}
}
public class ActorMap : ClassMap<Actor>
{
public ActorMap()
{
Id(x => x.ActorId, "actor_id");
Map(x => x.FirstName, "first_name");
}
}
The code to run it
var films = session.QueryOver<Film>().Where(x => x.FilmId < 5).Future();
foreach (var film in films)
{
foreach (var actor in film.FilmActor) //Does a batch query
{
Console.Write(actor.Actor.FirstName + ", ");
//For each actor it fetches the record from db
}
}
When i fetch the data from Actor for each actor a single query is fired. I would like nHibernate to do a In-Query to be fired for Actor as well. Which will look like the following
SELECT actor_id, first_name
FROM actor
WHERE actor_id in (actor_id batch collected from film.FilmActor )
I do not want to do a join between the filmActor and Actor in the batch, since that is proving to be costly.
How to load the one to one map/reference to do a batch fetch
You are almost there. All the
batch-size
mapping you've done is correct, but BatchSize could be set not only to the Collections, but toclasses
as well.NOTE: My experience is to use it almost for every class, exactly for a reason you mentioned here... We'll gain lazy loading with more effective batching