Archive

Posts Tagged ‘cascade’

NHibernate and the dark secrets of unneeded cascaded updates

September 23rd, 2011

I was having trouble with NHibernate cascading an update to a related entity regardless of cascade=”none”. During my investigation I learned that when you pass an entity to ISession.Save, .Update or .SaveOrUpdate, NHibernate goes through the entire object graph and updates persisted entities if it thinks they’re “dirty” – as in a changed state compared to the cached one – regardless of your cascade setting. NHibernate has several ways to determine whether an object is dirty, the last resort one being comparing the values of each individual field of an entity.

Turns out, I had my mapping screwed up. My related entity Y had two enumeration fields that I’d mapped as Int32. This throws off the comparisons. Int32.Equals doesn’t consider an enum of value 0 equal to the Int32 value 0. So every time I saved an entity referring to Y it considered entity Y dirty and updated it. Solution: in your mapping file, define the type of the field as the actual enumeration type or just leave the type out and let NHibernate figure it out.

bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Technology ,