Вопрос

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.

Это было полезно?

Решение

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top