October 25, 2011

NHibernate 3.3 XML Identity Mapping

Better late than never, a minor improvement requested in 2007 (!) is implemented in the upcoming NHibernate 3.3 release. It will finally be possible to map an identity generator using a small shortcut:

Instead of:

   1:  <class name="Customer">
   2:   <id>
   3:    <generator class="identity" />
   4:   </id>
   5:   <property name="Name" />
   6:  </class>

In 3.3:

   1:  <class name="Customer">
   2:   <id generator="identity" />
   3:   <property name="Name" />
   4:  </class>

This shortcut can only be used for generators that don't require parameters, or where the generator's parameter defaults are acceptable.

While we are on the subject of mappings, why not consider by-code mappings - new in 3.3:

   1:  mapper.Class<customer>(rc =>
   2:  {
   3:   rc.Id(x => x.Id, m => m.Generator(Generators.Identity));
   4:   rc.Property(x => x.Name);
   5:  });

More on by-code mappings can be found here, here and here.

No comments:

Post a Comment