如何在将子集合实体加入关联时急切地获取子集合

发布于 2024-09-05 08:33:33 字数 694 浏览 9 评论 0原文

假设以下虚构布局,

Dealership
  has many Cars
     has a Manufacturer

我想编写一个查询,表示为我提供名称为 X 的经销商并获取汽车系列,但在执行此操作时使用针对制造商的联接。我认为这需要使用 ICriteria。我在想这样的事情..


var dealershipQuery = Session.CreateCriteria< Dealership>("d")
                             .Add(Restrictions.InsenstiveLike("d.Name", "Foo"))
                             .CreateAlias("d.Cars", "c")
                             .SetFetchMode("d.Cars", FetchMode.Select)
                             .SetFetchMode("c.Manufacturer", FetchMode.Join)
                             .UniqueResult< Dealership>();

但生成的查询看起来与我预期的完全不同。我开始认为某个地方可能需要 DetachedCriteria,但我不确定。

想法?

Assuming the following fictional layout

Dealership
  has many Cars
     has a Manufacturer

I want to write a query that says get me a Dealership with a Name of X and also get the Cars collection but use a join against the manufacturer when you do so. I think this would require usage of ICriteria. I'm thinking something like this..


var dealershipQuery = Session.CreateCriteria< Dealership>("d")
                             .Add(Restrictions.InsenstiveLike("d.Name", "Foo"))
                             .CreateAlias("d.Cars", "c")
                             .SetFetchMode("d.Cars", FetchMode.Select)
                             .SetFetchMode("c.Manufacturer", FetchMode.Join)
                             .UniqueResult< Dealership>();

But the resulting query looks nothing like I would have expected. I'm starting to think a DetachedCriteria may be required somewhere but I'm not sure.

Thoughts?

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

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

发布评论

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

评论(1

标点 2024-09-12 08:33:33

在同一查询中获取集合几乎从来都不是最好的解决方案。

此链接详细介绍了最佳方法之一:http://ayende.com/Blog/archive/2010/01/16/eagerly-loading-entity-associations-efficiently-with-nhibernate.aspx

就我个人而言,我首选的解决方案是要在集合和实体中使用batch-size,请设置为我的默认页面长度的大小。这样,上面的查询将使用 3 个廉价的、可单独缓存的查询来完成,而不是使用一个昂贵的查询。

Fetching collections in the same query is almost never the best solution.

One of the best approaches is detailed in this link: http://ayende.com/Blog/archive/2010/01/16/eagerly-loading-entity-associations-efficiently-with-nhibernate.aspx

Personally, my preferred solution is to use batch-size in both the collections and the entities, set to the size of my default page length. That way, your query above would be done with 3 cheap, individually cacheable queries, instead of an expensive one.

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