开发者

Eager loading problem

开发者 https://www.devze.com 2023-03-10 18:11 出处:网络
I\'ve a problem with this linq to nhibernate query var listeShopping = (from cart in session.Query<Cart>()

I've a problem with this linq to nhibernate query

var listeShopping = (from cart in session.Query<Cart>()
    .Fetch(cart => cart.ItemShopping)
    .ThenFetch(item => item.Manufacturer)
    select cart.ItemShopping).ToList<ItemShopping>();

When I launch it I've a strange error :

Query specified join fetching, but the owner of the fetched association was
not present in the select list [FromElement{explicit,not a collection join,
fetch join,fetch non-lazy properties,classAlias=_1,role=,tableName= (...)

I need the eager loading, how can I avoid that error ? If it can help, I'll mention that I use the cart table only like a inner join table. I just need to know the ItemShopping in the Cart.

Regards

Edit

I modified the code to make it readable in english. I corrected the error.

Edit 2

I found that method, it seems to work ... Can someone check it if I didn't make an error ?

        var list = (from item in session.Query<ItemShopping>()
                   .Fetch(item => item.Manufacturer)
                   from cart in item.Cart
        开发者_如何学JAVA           select item).ToList<ItemShopping>();


I don't think it's possible to handle this scenario with Linq. But it is with HQL:

var listeShopping = session.CreateQuery(@"
    select item
    from Cart cart
    join cart.ItemShopping item
    join fetch item.Manufacturer
    ")
    .List<ItemShopping>();

Side note: eager fetching the Manufacturer this way is not necessarily the best performing approach. Consider using batch-size in the Manufacturer class instead.

0

精彩评论

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

关注公众号