问题描述
我有以下映射
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");
}
}
运行它的代码
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
}
}
当我从Actor中获取每个actor的数据时,将触发一个查询.我希望nHibernate也可以为Actor触发In-Query.如下图所示
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 )
我不想成批地加入filmActor和Actor之间的联接,因为事实证明这很昂贵.
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
推荐答案
您快到了.您完成的所有batch-size
映射都是正确的,但不仅可以将CollectionSize设置为Collections,还可以将其设置为classes
.
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 to classes
as well.
public ActorMap()
{
Id(x => x.ActorId, "actor_id");
Map(x => x.FirstName, "first_name");
BatchSize(100);
}
注意:我的经验是,几乎每个班级都使用它,正是出于您在这里提到的原因...我们将通过更有效的批处理获得延迟加载
这篇关于如何在Fluent nHibernate中进行一对一映射以尊重父对象/所有者对象的批处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!