Question

I have a scenario that i want to add some standard properties to my entities. Meaning that i will have e.g. 1 int and 2 string properties applied to all relevant entities. I have over 100 mapping files and most but not all will be hosts to these new properties. In the classes its easy to define this; in the mappings however i've found no reference other than creating a utility or xslt and applying that (How to define reusable <generator> elements in hibernate mapping).

However i want to be able to add/modify/remove properties from this "standard" mapping.

thx for any replies

Edit1: an example of the mapping i want to add

<property name="TimeOfEdit" column="TimeOfEdit" type="DateTime" not-null="true"/>
<many-to-one name="EditedBy" column="FK_EditedBy" cascade="save-update" not-null="true" />

Edit2: I removed the accepted solution because with NH 2.1.1 XML Entities are not working (NH-1236) and NH will throw a "DTD is prohibited in this XML document"

Was it helpful?

Solution 5

It seems that the only to do this, is to use Dynamic Mapping (http://ayende.com/Blog/archive/2008/05/01/Dynamic-Mapping-with-NHibernate.aspx)

as such since i've already defined an interface that my entities will use for the new properties (lets say IAuditable) its just a matter of running the appropriate code at the NH-session initialization

Configuration cfg = new Configuration() Mappings mappings = cfg.CreateMappings(); 
foreach (var persistentClass in mappings.Classes) 
{ 
   if (persistentClass.MappedClass is IAuditable)
   {
     ...
   }
}

and then

cfg.BuildSessionFactory();

to have it wired up and ready to be used for about 85 classes the performance impact is negligible

OTHER TIPS

It depends on how these properties are implemented in your classes.

If they are all defined in a base class or interface, you could map them once in the base class or interface, and derive using union-subclass. There are some limitations. Read this chapter in the NHibernate documentation about it.

If you decide to put them together into a class, you could map them as a user type. This will be similar to a component, but you could specify some things like type names, lengths and others in the user type. You still need to specify each column name.

There is another option: you could use XML entities. This is a rather primitive feature from XML which is supported by NHibernate. Read this chapter in the NH reference documentation where it is mentioned.

Creating a special code generator for your specific case is your only option.

Option 1:
-Define these 3 properties in a base class

-have your entities inherit from this base

-set up 'table per class hierarchy'

Option 2:

-Define these 3 properties as a component.

-You can have the mapping for these 3 properties in one file that is reused.

You might take a look at fluentNHibernate, It will simplify the mapping work for you. With With auto mapping you may only need an abstract base class to define these properties.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top