Nhinerbate 参考实体的延迟加载

发布于 2024-10-04 00:37:03 字数 559 浏览 2 评论 0原文

我有这样的场景:

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 个选择。就像延迟加载不起作用一样。

我的问题是:

  1. 如何让延迟加载在这里工作?
  2. 我可以在单个查询中包含 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:

  1. How to I make the lazyload work here ?
  2. Can I bring the A entities and B entities in a single query ?

Thank you,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜未央樱花落 2024-10-11 00:37:03

延迟加载默认情况下处于打开状态并且实际上应该可以工作。如果出现问题,例如无法生成 B 类的代理,则在创建会话工厂时会抱怨。

你确定对B的查询是查询本身完成的,而不是后续对A的访问?

您可以通过两种方式优化对 B 的访问:在单个查询中将它们与 A 一起获取。 (我不知道 Fluent,这是配置它的 xml 方式:)

<many-to-one fetch="join" ...>

这在与列表一起使用时会出现一些问题,并且还可能会严重破坏您的查询。当然,这根本不是延迟加载。

另一个非常好用且强大的优化是批量获取。它允许在单独的查询中获取实例,但同时获取其中的多个实例。

<class name="B" batch-size="20" ...>

这将在一个查询中一次获取 20 个 B。它也可用于列表:

<one-to-many fetch-size="20" ...>

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:)

<many-to-one fetch="join" ...>

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.

<class name="B" batch-size="20" ...>

This would fetch 20 B's at once in one query. It is also available for lists:

<one-to-many fetch-size="20" ...>
说不完的你爱 2024-10-11 00:37:03

扩展 Stafan 使用批量大小的建议,快速 Google 搜索显示 Fluent NHibernate 现在支持 BatchSize 查询。来自文档:

ClassMap<T> BatchSize(int size) 

Sets the query batch size for this entity.

我自己从未使用过它,并且文档很少(就像很多 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:

ClassMap<T> BatchSize(int size) 

Sets the query batch size for this entity.

Never used it myself, and the documentation is minimal (like a lot of FNH), but maybe you can find some sample code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文