R – Fluent NHibernate: override derived classes not in the base class auto-mapping

fluent-nhibernate

The story:
I had class User and class Organization: User. I did not use any mappings for these classes, let FNH do mapping automatically. Then, I added

   public class OrganizationMap : IAutoMappingOverride<Organization>
   {
      public void Override(AutoMap<Organization> mapping)
      {
      }
   }

Notice there're no overrides. So I did not expect any changes in FNH behavior. But I got this (during schema export actually):

NHibernate.MappingException:
(XmlDocument)(2,4): XML validation
error: The element 'class' in
namespace 'urn:nhibernate-mapping-2.2'
has incomplete content. List of
possible elements expected: 'meta,
subselect, cache, synchronize,
comment, tuplizer, id, composite-id'
in namespace
'urn:nhibernate-mapping-2.2'.

The generated Orders.Core.Organization.hbm.xml was really empty:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="">
  <class name="Orders.Core.Organization, Orders.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="Organizations" xmlns="urn:nhibernate-mapping-2.2" />
</hibernate-mapping>

So, after I reviewed the User.hbm I got the idea – I need to override Organization in the base class like this:

   public class UserMap : IAutoMappingOverride<User>
   {
      public void Override(AutoMap<User> mapping)
      {
         mapping.JoinedSubClass<Organization>("ColumnId", m => {...}
         );
      }
   }

But, I would better like to do this in a separate mapping override class for Organization… after all, what would the mapping become if I have 5 subclasses all in single Override method.

Is this possible?

Best Solution

Your override is telling FNH that you will manually write the mappings for that class. The error you are getting is because there is nothing being mapped for Organisation (if you look at the generated HBM.xml it will be empty).

What exactly are you wanting to write the override for?

Edit:

In that case, you can do something like this:

public class MyAlteration : IAutoMappingAlteration
{
    public void Alter(AutoPersistenceModel model)
    {
        model.ForTypesThatDeriveFrom<User>(
            map => map.HasMany<User>( x => x.Children)
        );
    }       
}

And when configuring fluent nhibernate:

model.Alteration( a => a.Add<MyAlteration>());

Note: This is using the latest codebase of fluent nhibernate (1.0RC).