Prevent free access to NHibernate n + 1

I have a fairly deep graph of objects (5-6 nodes), and when I cross parts of it, NHProf tells me that I have a "Select N + 1" problem (which I do).

Two solutions that I know of

  • Waiting for children to load
  • Share my graphic object (and load the load)

I really don't want to do any of them (although I can split the graph separately later, as I see it)

Presently....

Is it possible to tell NHibernate (with FluentNHibernate) that whenever I try to access children to download them all at once, instead of select-n + 1-ing when I iterate over them?

I also get an "unlimited result set", which seems to be the same problem (or rather, will be solved using the above solution, if possible).

Each daughter collection (across the entire schedule) will have only about 20 members, but 20 ^ 5 is a lot, so I don’t want to download everything when I get the root, but just get the whole child when I get closer to it.

Edit: an afterthought .... what if I want to present paging when I want to display children? Do I have the opportunity to break my object chart here or is there some kind of secrecy that I can use to solve all these problems?

+5
select nhibernate fluent-nhibernate
source share
2 answers

It seems to me that you want to use the approach to using your domain model, rather than creating a specific nhibernate request to process this script. Given this, I suggest you take a look at the batch size attribute, which you can apply to your collections. The free Fluent NHibernate interface does not yet support this attribute, but as a job you can use:

HasMany(x => x.Children).AsSet().SetAttribute("batch-size", "20") 

Given the general lack of information about your specific scenario, I can’t say for sure whether the batch size is the ideal solution, but I certainly recommend that you give preference. If you have not already done so, I suggest you read the following:

http://www.nhforge.org/wikis/howtonh/lazy-loading-eager-loading.aspx

http://nhibernate.info/doc/nhibernate-reference/performance.html

NHibernate performance documentation will explain how batch size works.

Edit: I do not know how to go to a page from your domain model. I recommend that you write NH queries for scenarios that require paging.

+9
source share

Edit: an afterthought .... what if I want to introduce paging when I want to provide children? Do I have a gap in my object here, or is there some sneakyness I can use to solve all these problems?

Well, if you download only children, you can list them :). But if you want something like: LoadParent AND PageChildren, then I don't think you can do it.

0
source share

All Articles