0

I need (for a real good reason, trust me) to load what's almost the entire graph of my database using NHibernate. There are not so many entities, but the graph is kind of convoluted.

My graph looks something like :

  • EntityA
  • |
  • --> EntityB
  • |
  • --> List
    • |
    • --> EntityD
    • |
    • --> List
      • |
      • --> EntityF

Well... You get the idea.

I would like to load all of it with as few queries and roundtrips as possible, and possibly no select N+1 at all. Also I want to keep it as a graph, so I can easily loop through it later.

What's my best option? NHibernateUtil.Initialize()? Fetch()/FetchMany()? Future/MultiQuery?

I'm kind of lost, but I guess I'll have to do it in multiple operations. But what would the most efficient?

And bonus : all the entities have a IsPublished property. I want to be able to load either all the entities or only those that are published.

EDIT

In the end I tried this :

var applicationFields = NHibernateSession.Current.Query<ApplicationField>().Where(af => af.Ispublished)
.FetchMany(af => af.Illustrations)
.ThenFetch(i => i.InteractiveConfiguration)
.ThenFetchMany(ic => ic.UniqueSellingPoints)
.ThenFetchMany(usp => usp.Pictures)
.ToList();

Considering the way my graph looks, I think it's quite OK. Also I don't have multiple collections on same level, which is what induce cartesian products as far as I know.

For those who asked, it's not a recursion problem. Otherwise I would be using SQL Server CTE. Also I'm not updating any entity; it's just plain read (for an offline app).

But I'm clueless for my "bonus" question. I know EF can load partial collections based on filters, but it doesn't seem possible with NH.

  • Possible duplicate http://stackoverflow.com/questions/2167866/eager-loading-a-tree-in-nhibernate – autonomatt Jan 27 '12 at 19:47
  • Can you post your current mapping and class, I assume that all entities are from the same type. – Mr Mush Jan 27 '12 at 20:44
  • Are you going to be updating many of the entities that you load? Are you loading all this data to pull summary type information? A view or nhibernate query may help with this. It really depends on how you are using the data in my opinion. – Cole W Jan 28 '12 at 20:05

1 Answers1

1

short answer: a combination of Fetch()/FetchMany() and Future() depending on the usecase and expected list-sizes

Firo
  • 30,626
  • 4
  • 55
  • 94