这篇文章:
http://kennytordeur.blogspot.com/2011/04/nhibernate-in-combination-with_06.html
描述如何从数据库以外的资源加载实体,在本例中为webservice。这很棒,但是如果我在一个查询中加载多个客户端,每个客户端都有不同的MaritialState,那么它将不得不为每个客户端调用for服务。有没有一种方法可以预先加载所有的婚姻状态,这样它就不需要为每个客户端来回访问way服务了?
发布于 2011-09-07 11:00:46
我不认为Hibernate支持这一点。“n+1选择问题”是一个众所周知的问题,Hibernate有相当多的策略来处理它(批处理、子选择、急切获取等)。问题是你有“n+1 web服务调用”,所有这些机制都是无用的。Hibernate根本不知道您在IUserType中做了什么。它假定您正在转换已经加载的数据。
看起来你必须实现你自己的预加载。如下所示:
// TODO: not thread safe, lock or use ConcurrentDictionary
static IDictionary<Int32, ClientDto> _preLoadedClients
= new IDictionary<int,ClientDto>();
public Object NullSafeGet(IDataReader rs, String[] names, ...) {
Int32 clientid = NHibernateUtil.Int32.NullSafeGet(rs, names[0]);
// see if client has already been preloaded:
if(_preLoadedClients.ContainsKey(clientid)) {
return _preLoadedClients[clientid];
}
// load a batch: clientId + 1, client + 2, ... client + 100
var batchOfIds = Enumerable.Range(clientid, 100);
var clientsBatch = clientService.GetClientsByIds(batchOfIds);
_preLoadedClients.Add(clientsBatch);
return _preLoadedClients[clientid];
}https://stackoverflow.com/questions/7322874
复制相似问题