Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to suppress the Update after Insert in NHibernate?

Tags:

nhibernate

I've been thrown into researching a database performance issue with a new application using NHibernate. I noticed that on some tables NH does an insert of a row followed by an update of the same row with exactly the same data. What I have gathered so far is that updates after inserts are done on tables that have many-to-one relationships defined.

In any case, I would have expected that NH figures that the data is the same and suppresses the extra update. Is there a way to suppress this extra update at all?

like image 847
Rob Avatar asked Nov 05 '09 20:11

Rob


People also ask

What is the difference between NHibernate and fluent NHibernate?

Fluent NHibernate offers an alternative to NHibernate's standard XML mapping files. Rather than writing XML documents, you write mappings in strongly typed C# code. This allows for easy refactoring, improved readability and more concise code.

Why is NHibernate used?

NHibernate is an ORM (Object Relational Mapper). Its purpose is to map objects in your OO application to tables in a database for persistence. Why would you need it? Because it can save you from writing a lot of tedious ADO.NET code.


1 Answers

It sounds like you have a bidirectional relationship on a many-to-one association.

The solution to that problem is to add inverse="true" on the one-to-many side of the relationship. Here is an example.

The insert is performed by the child saving its data. The update is performed by the parent setting the parentId of the child record in the database. If the the relationship was uni-directional, the first insert would not have the parentId and the update would be required. With a bi-directional relationship, the update is superfluous as you describe. The inverse="true" tells the parent that the child is responsible for maintaining the relationship thus preventing the extra update.

like image 102
g . Avatar answered Sep 18 '22 03:09

g .