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

Posted by ShaneC on Stack Overflow See other posts from Stack Overflow or by ShaneC
Published on 2010-06-10T21:11:50Z Indexed on 2010/06/10 21:12 UTC
Read the original article Hit count: 319

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?

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about hql