开发者

How to eager fetch a child collection while joining child collection entities to an association

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 somewher开发者_JS百科e but I'm not sure.

Thoughts?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜