Tuesday, 10 September 2013

nHibernate Asp.net MVC - Updating detached entities - How to avoid that extra DB hit

nHibernate Asp.net MVC - Updating detached entities - How to avoid that
extra DB hit

I am using nHibernate as ORM with an ASP.NET MVC application. The ORM is
configured to behave as Session per request. The problem I am facing is
during update.
We have a grid that displays customers
· A user clicks on a row and chooses to edit that row.
· The user modifies the address for the customer and clicks save
· The code loads up a customr object and attaches a new address object to
the customer with the id coming from the selected value of the address
dropdown and a version of 1 (we have to modify the id and version to be
something other than the unsaved-value so nhibernate does not think the
address object is unsaved)
· The code saves the customer object with nhibernate and this customer
object is placed into the nhibernate session
· The code then gets all of the customers to display in the grid
· The grid displays all the customers and the customer we just saved is
displayed with no address description since we attached a address object
to the country that only had an id and version when saving
To avoid this issue we could get the address object from the database and
attach it to the customer prior to the save but this would require a db
transaction when you would not think one would be needed.
Is there a better way to achieve this?

No comments:

Post a Comment