views:

1492

answers:

2

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?

+1  A: 

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).

Doolwind
What I would like is to override subclasses in their own mapping classes, that is inside OrganizationMap say smth like "IsSubclassOf<User>()".
queen3
+1  A: 

Turned out that with latest FNH (some revision after RC) this is possible now. I wonder if this is because I asked ;-)

So I had this

 mapping.JoinedSubClass<Organization>("UserId", m =>
    {
       m.HasMany(x => x.Currencies).Element("Currency").AsBag();
    }
 );

and it stopped working after upgrading to RC. Then I moved this into its own class

   public class OrganizationMap : IAutoMappingOverride<Organization>
   {
      public void Override(AutoMapping<Organization> mapping)
      {
         mapping.HasMany(x => x.Currencies).Element("Currency").AsBag();
      }
   }

it started to work again. Just like I wanted! Now I don't even need to indicate JoinedSubClass as this is the default, anyway. I can just override my subclass properties which is cool.

Though it wasn't too easy to figure out why NH started to complain about association of strings... I even thought that .Element is broken in RC. I wonder why JoinedSubClass still has this mapping part if it doesn't completely work.

queen3

related questions