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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With