Ayende @ Rahien

Refunds available at head office

NH Prof new feature: Superfluous update

Yes, I am aware that I said that I would only have two more feature for NH Prof before releasing. But I am currently being held hostage by the new features fairy, and negotiations over a feature freeze seems to have gotten to a stand still. Beside, it is a neat feature.

The actual feature is quite simple. Let us say that we have the following model:

image 

Notice that this is a very common case of bidirectional association, and this is mapped to the following table model:

image

Notice that while on the object model this is a bidirectional association and is maintained by two different places, it is maintained on a single place in the database.

This is a a very common case, and quite a few people get it wrong. By default, NHibernate has to assume that it must update the column on both sides, so creating a new post and adding it to the Blog’s Posts collection will result in two statements being written to the database:

   1: INSERT INTO Posts(Title,
   2:                   Text,
   3:                   PostedAt,
   4:                   BlogId,
   5:                   UserId)
   6: VALUES     ('vam' /* @p0 */,
   7:             'abc' /* @p1 */,
   8:             '1/17/2009 5:28:52 PM' /* @p2 */,
   9:             1 /* @p3 */,
  10:             1 /* @p4 */);
  11: select SCOPE_IDENTITY ( )
  12:  
  13: UPDATE Posts
  14: SET    BlogId = 1 /* @p0_0 */
  15: WHERE  Id = 22 /* @p1_0 */

As you can see, we are actually setting the BlogId to the same value, twice.

Now, there is a very easy fix for this issue, all you have to do is to tell NHibernate on the Blog’s Posts mapping that this is a collection where the responsibility for actually updating the column value is on the other side. This is also something that I tend to check in code reviews quite often. The fix is literally just specifying inverse=’true’ on the <many-to-one> association.

And now NH Prof will detect and warn about such cased:

image

Beautiful!

This is also the first case in which I am starting to do much more in depth analysis of what is actually going on with NHibernate. I planned to do this sort of thing after the v1.0 release, but as I said, I am held hostage by the new features fairy, and this is my negotiation technique :-)

Comments

Tobin Harris
01/18/2009 11:16 AM by
Tobin Harris

Great feature! I love the direction this is taking, and that NHProf can start to actually teach people useful information about NHibernate. That also gives them 2 reasons to buy your product - to help find possible performance problems and also to learn how to resolve them.

Kenny Eliasson
01/18/2009 01:31 PM by
Kenny Eliasson

This is awesome! One of the first issues I believe anyone has with NHibernate is the mapping of collections. Great work! Keep it up

Dave
01/18/2009 03:31 PM by
Dave

I really like the alerts NHProf is getting. So far, profiling (such as JetBrains dotTrace or RedGate ants) usually 'pauses' the moment the application (or depended component) performs a database query. All we know is that DbCommand.ExecuteQuery took 120ms. We offen tent to try to minimize those performance bumps. Because we are confidant that our queries are very optimized, that those areas are usually skipped during profiling sessions. Most developers use tools like Microsoft query analyzer to see if they have made the proper indexes.

Because the update is unnecessary, such a alert can be a (literally) real time safer when you're dealing with a customer <--> order association where both tables have more than 10 million records.

In today's applications databases play an important part. Compared to the profilers mentioned above 200 (or less) euros for a query profiler/debugger/optimizer is a very good bargain. A must have for every .net developer!

Stefan
01/19/2009 12:21 AM by
Stefan

"The fix is literally just specifying inverse=’true’ on the <many-to-one association."

Am I missing something here or just miss reading it, say in my example Inverse="true" goes on my set mapping:

<set

This is the correct side right? I think of this as the one-to-many side but I may have just misinterpreted my definitions :).

Cheers

Stefan

Ben
01/21/2009 10:35 AM by
Ben

Does this profiler work with the Java version of Hibernate?

Ayende Rahien
01/21/2009 12:52 PM by
Ayende Rahien

Ben,

There is a high likelyhood that it would,as a matter of fact.

Never tested it, though.

Comments have been closed on this topic.