开发者

How to fetch objects multiple levels deep and wide with NHibernate?

开发者 https://www.devze.com 2023-04-04 02:52 出处:网络
I have a case where I need to load about 10 000 objects from database. The data model is something like this:

I have a case where I need to load about 10 000 objects from database. The data model is something like this:

public class SimulationObject
{
    public Container Container {get;set;}
    public IList<Re开发者_开发问答sultItem> Results {get;set;}
    public IList<PreviewData> PreviewData {get;set;}
    ...
}

public class ResultItem
{
    public IList<SomeDataItem> Items {get;set;}
    ...
}

public class PreviewData
{
    public IList<SomeDataItem> Items {get;set;}
    ...
}

This means that I want to query a list of SimulationObjects from database according to some query, and all of it's properties (references) and subitems.

The quantities of collections are the following:

  1. SimulationObject - ca 6000 - 1200, depending on parameters in "where"
  2. SimulationObject.Results - ca 5 to 40 items
  3. SimulationObject.Results.Items - ca 0 to 2 items
  4. SimulationObject.PreviewData - ca 0 to 2 items
  5. SimulationObject.PreviewData.Items - ca 1 to 3 items

Normally, I'd do it like this:

var query = from sim in session.Query<SimulationObject>()
        where sim.Container.Id == containerId && ...
            select sim;

query = query.FetchMany(c => c.Results).ThenFetch(o => o.Items)...

However, I also need to fetch "PreviewData" items for that would create a Cartesian product in my query (meaning PreviewDataAndSubItemsCount x ResultsAndSubItemsCount amount of rows returned) which is very ineffective. Also, as I need to load a LOT of SumulationObjects (about 10000 as sayd earlier), I cannot do lazy loading (10000 queries..and there are other difficulties too, so this isn't even an alternative to consider).

So what are the alternatives ? What strategy would you use to load a complite object graph into memory ?

Thanks.


Ayende explains one way to do this here (HQL):
http://ayende.com/blog/4367/eagerly-loading-entity-associations-efficiently-with-nhibernate

There was also a StackOverflow article that I came across that does this using QueryOver:
NHibernate Eager Loading with Queryover API on a complex object graph

I would be very interested in getting additional input from the rest of hte community though. I think this is one area in which NH could improve on.

0

精彩评论

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

关注公众号