Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate SaveOrUpdate, error: No persister for

I have a NHibernate mapping for a base class A

class A
{
}

class B : A
{
}

public save(A a)
{
 session.SaveOrUpdate(a);
}

Error: No persister for B

As you can see B has been passed with the correct base type A, but i still get the error about the persister for type B

Does NHibernate support inheritance like this... what can i do?

like image 631
John Avatar asked Oct 27 '09 01:10

John


1 Answers

Update: rewritten answer

Apparently, though I fail to find the definitive source on that, is the actual class important. This makes sense, if you consider that NHibernate uses reflection to find the underlying type. Also, when the mapping is loaded, it decorates your types, so basically they become different types altogether (you can see that when you hover over them while debugging).

This principle basically prevents inheritance downcast-mapping, as your derived type is not mapped it is not decorated and hence not known. If you need to use inheritance, you have a few options:

  1. Make a mapping for the derived class
  2. Add a converter for your class to copy from one type in the other (cast is not enough, it won't change the underlying object)
  3. Use ICloneable

All these methods are rather cumbersome. If your design allows it, instead of inheritance, use partial classes or extension methods. The latter is what I find in my own projects, apparently I ran into this before and made it a custom to use extension methods.

like image 89
Abel Avatar answered Oct 25 '22 10:10

Abel