0
votes

I have simple domain with Order and OrderLines. Is it possible to load the Order and associated OrderLine objects without a join? I'm trying to use the Future/FutureValue to perform two simple queries. I'm hoping that NHibernate knows how to combine these in the cache. I'm using NHibernate 3.2 with code only mapping.

So far here is what I have:

    // Get all the order lines for the order
    var lineQuery = session.QueryOver<OrderLine>()
        .Where(x => x.WebOrder.Id == id).Future<OrderLine>();

    // Get the order
    var orderQuery = session.QueryOver<WebOrder>()
        .Where(x => x.Id == id)
        .FutureValue<WebOrder>();

    var order = orderQuery.Value;

This works as expected sending two queries into the database. However when I use loop to go through order.OrderLines NHibernate send another query to get the order lines. My guess is that because I'm using constraints (Where(x => ...) NHibernate doesn't know how to get the objects from the session cache.

Why do I want to do this without join

I know I can use Fetch(x => x.OrderLines).Eager but sometimes the actual parent (in this case Order) is so large that I don't want to perform the join. After all the result set contains all the order columns for each orderline if I perform the join. I don't have any raw numbers or anything I'm just wondering if this is possible.

1

1 Answers

0
votes

it's quite possilbe. see nHib's fetching strategies.
you can choose either 'select' (if you're only dealing with one Order at a time) or 'subselect'.