Nhinerbate 参考实体的延迟加载
我有这样的场景:
class A
{
public virtual int Id { get; set; }
public virtual B Child { get; set; }
}
class B
{
public virtual int Id { get; set; }
}
在 A 类的映射中,我引用了 B 类:
map.Reference(a => a.Child).LazyLoad();
现在,当我执行以下操作时:
Session.Query<TypeOfA>().Select(a => a);
除了正常的 select * from ATable 之外,我还从 BTable 中为每条 A 行获取 n 个选择。就像延迟加载不起作用一样。
我的问题是:
- 如何让延迟加载在这里工作?
- 我可以在单个查询中包含 A 实体和 B 实体吗?
谢谢你,
I have this scenario:
class A
{
public virtual int Id { get; set; }
public virtual B Child { get; set; }
}
class B
{
public virtual int Id { get; set; }
}
In the mapping of class A, I have a reference to class B:
map.Reference(a => a.Child).LazyLoad();
Now when I do something like:
Session.Query<TypeOfA>().Select(a => a);
Apart from the normal select * from ATable I get n selects from the BTable for each A line. Is like lazy loading is not working.
My questions are:
- How to I make the lazyload work here ?
- Can I bring the A entities and B entities in a single query ?
Thank you,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
延迟加载默认情况下处于打开状态并且实际上应该可以工作。如果出现问题,例如无法生成 B 类的代理,则在创建会话工厂时会抱怨。
你确定对B的查询是查询本身完成的,而不是后续对A的访问?
您可以通过两种方式优化对 B 的访问:在单个查询中将它们与 A 一起获取。 (我不知道 Fluent,这是配置它的 xml 方式:)
这在与列表一起使用时会出现一些问题,并且还可能会严重破坏您的查询。当然,这根本不是延迟加载。
另一个非常好用且强大的优化是批量获取。它允许在单独的查询中获取实例,但同时获取其中的多个实例。
这将在一个查询中一次获取 20 个 B。它也可用于列表:
Lazy loading is switched on by default and should actually work. If there would be a problem, for instance if it can't generate the proxy for class B, it would complain when creating the session factory.
Are you sure that the queries for B are done by the query itself, and not be subsequent access to A?
You could optimize the access to B in two ways: fetch them together with A in a single query. (I don't know fluent, this is the xml way to configure it:)
This has some problems when used with lists and could also blow up your query a lot. It is of course not lazy loading at all.
Another, very nice and powerful optimization is batch fetching. It allows the instances to be fetched in separate queries, but fetches several of them at once.
This would fetch 20 B's at once in one query. It is also available for lists:
扩展 Stafan 使用批量大小的建议,快速 Google 搜索显示 Fluent NHibernate 现在支持 BatchSize 查询。来自文档:
我自己从未使用过它,并且文档很少(就像很多 FNH 一样),但也许您可以找到一些示例代码。
Expanding on Stafan's suggestion to use batch-size, a quick Google search reveals that Fluent NHibernate now supports BatchSize for queries. From the docs:
Never used it myself, and the documentation is minimal (like a lot of FNH), but maybe you can find some sample code.