NH Prof new featureSuperfluous 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:
Notice that this is a very common case of bidirectional association, and this is mapped to the following table model:
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 Posts14: 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:
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 :-)
More posts in "NH Prof new feature" series:
- (09 Dec 2010) Alert on bad ‘like’ query
- (10 Dec 2009) Filter static files
- (16 Nov 2009) Exporting Reports
- (08 Oct 2009) NHibernate Search Integration
- (19 Aug 2009) Multiple Session Factory Support
- (07 Aug 2009) Diffing Sessions
- (06 Aug 2009) Capturing DDL
- (05 Aug 2009) Detect Cross Thread Session Usage
- (22 May 2009) Detecting 2nd cache collection loads
- (15 May 2009) Error Detection
- (12 May 2009) Queries by Url
- (04 Feb 2009) View Query Results
- (18 Jan 2009) Superfluous <many-to-one> update
- (18 Jan 2009) URL tracking
- (10 Jan 2009) Detecting distributed transactions (System.Transactions)
- (06 Jan 2009) The Query Cache
- (05 Jan 2009) Query Duration
- (24 Dec 2008) Unbounded result sets
- (24 Dec 2008) Row Counts
Comments
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.
This is awesome! One of the first issues I believe anyone has with NHibernate is the mapping of collections. Great work! Keep it up
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!
"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
Yes.
Does this profiler work with the Java version of Hibernate?
Ben,
There is a high likelyhood that it would,as a matter of fact.
Never tested it, though.
Comment preview