0
votes

I've run into a strange error in fluent nhibernate. I have a basic set of classes that inherit from interfaces, and I have mapped the interfaces to tables, and where classes utilize these, they implement a generic of the interface. They are defined as follows;

interface IPage {
  // properties
}

class IPageMap : ClassMap<IPage> {
 // mapping information
}

class Page : IPage {
 // IPage Implementation
}

class ITag {
  // properties
}

class ITagMap : ClassMap<ITag> {
  // mapping information
}

class Tag : ITag {
  // ITag implementation
}

class Template {
  virtual IList<IPage> Pages { get; set; }
  virtual IList<ITag> Tags { get; set; }
}

This should work fine. When I create an object like this ...

var pages = new List<IPage> {
   new Page {
     // ..
   }
}

pages.ForEach( x => { session.SaveOrUpdate(x); } ); // no exception here

var tags = new List<ITag> {
   new Tag { 
     // ... 
   }
}

tags.ForEach( x => { session.SaveOrUpdate(x); } ); // exception happens here.

var list = new List<Template> {
  new Template {
     Pages = new List<IPage> {
       new Page { 
          // ... 
       }
     },
     Tags = new List<ITag> {
       new Tag { 
          // .. 
       }
     }
   }
}

I am given the following exception.

There is no persister for Tag

Okay, I didn't define a persister for Tag, but I did for ITag, and Tag inherits it. IPage and Page work the same way. If I comment out the tag information and just leave the IPage and Page implementation, it works fine for those.

Why is ITag and Tag being treated differently? Any ideas?

1

1 Answers

2
votes

You need to map Tag as a subclass of ITag. Every persistent type needs to be mapped separately, you need to tell NH where the members of the subclass are going to (even if there aren't any) and more importantly which concrete type NH should instantiate when reading the data from the database.