Jeffrey Palermo (.com)

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
NHibernate: Casting is a state change and makes a persistent object dirty immediately - level 200

NHibernate knows when an object under its watch has changed.  As soon as the object changes, it is "dirty".  Some other changes might cause an object to be dirty as well.  One that my team recently encountered is a cast.  We use an enum of type byte.  It's only a few items (less than 255), so we use a tinyint in our database.  When our mapping uses type="byte", NHibernate casts from the byte to our Enum type when hydrating the object.  This cast is a change because when NHibernate checks the value, it's an Enum, not a byte. 

To get around this cast (implicit or not), we use the fully qualified type name of the Enum in the mapping.  NHibernate understands Enums natively, so just put in the enum type, and you are off to the races.  Note that if you are using an Enum that is nested inside a public class, you need to follow .Net's rules for fully-qualified type names.

See MSDN's documentation for this: 

http://msdn2.microsoft.com/en-us/library/w3f99sx1.aspx

TopNamespace.SubNameSpace.ContainingClass+NestedEnum,MyAssembly

Posted Mon, Oct 23 2006 7:06 AM by Jeffrey Palermo

[Advertisement]

Comments

Ayende Rahien wrote re: NHibernate: Casting is a state change and makes a persistent object dirty immediately - level 200
on Mon, Oct 23 2006 2:51 PM
You can just drop the type name completely, NHibernate will do the Right Thing here. Actually, this is the first time I hear about this issue, because I always leave the type name when it is obvious from the classes.
Jeffrey Palermo wrote re: NHibernate: Casting is a state change and makes a persistent object dirty immediately - level 200
on Mon, Oct 23 2006 3:23 PM
I've tried it both ways, and when going from "byte" to my C# enum, my object is dirty right away. Specifying the enum in the mapping gives me the correct behavior. I'll admin I didn't try it with a normal Enum that defaults to the "Int32" data type.