Saturday, 15 February 2014

c# - NHibernate always hydrates many-to-one -


I am experiencing something weird while using NHibernate. I have opened a codebase that I did not write, and who He's not here anymore. So I think that what I see is more tips on debugging than anything else.

I am using the NHINNET 3.4 Linux provider to query one unit, which has a lot of relation with another entity

Whatever I see is then Also, as long as I do not use the property which represents a number of relationships, it is always hydrated. By looking at questions through NHibernate profiler, I can see that the property is loading lazy. Setting Breakpoints with My Debugger I can see that the property has never been accessed.

When using the NHibernate profiler, I can see that when I load the Basic Entity List, but not in the same question

I do not know why this happens, But this results in N + 1 problem

Entities are defined in hbm.xml files.

So what I am asking is the input where I should start digging. I think that whatever I can think of already, I have tried to do all this.

"post-text" itemprop = "text">

The problem is not on NHibernate, it is on the use side (we both would agree I can say)

So, what we want (underlying or clear) is the initial point is the lazy setting:

  // class level // is sluggish by default & Lt; Class name = "entity" ... lazy = "true" ... & gt; ... // references multiple-to-one levels, by default also lazy & lt; Many-to-one names = "unit" lazy = "proxy" ... /> In order to avoid N + 1, we must ensure that we do not have the  batch-size  setting, 

  / / class / institution level // attention - it is not contained , We have to determine that & lt; Class name = "entity" ... lazy = "true" ... batch-size = "25" & gt; ... // collection level // attention - it is not rooted, we have to decide that & lt; Bag name = "Entities" ... batch-size = "25" & gt; ...  

On its place, NHibernate will not automatically load more than necessary. So what can be the case of general usage, when the context needs to be loaded?

When overlapping:

  public override baul par (object obj) {} public override Int GetHashCode () {}  

These methods should be overridden to provide a unique key to the business (for some time - for joint-id, even if necessary).

Last but not least

strong>, There may be a problem with your debugger. Debugger is actually affecting loading, because whenever we are looking at anything in the debug window ... we force NHBeNnet to load lazy stuff.

This is surprisingly the most frequent source of "forced" loading, then clear your supervisors ...

Finally, suggestions - just to load the original unit Unit Testing Finally, clear the session if you see some of the DB hits, see (with the profiler) you can also easily check what was the reason ...


No comments:

Post a Comment