Mapping inheritance in NHibernate 3.3

I have the inheritance described below:

public abstract class BaseEntity<TId> {....} public abstract class ModelEntity : BaseEntity<Int32>{....} public abstract class AuditableEntity : ModelEntity,IAuditable{....} public class ApplicationUser : AuditableEntity{....} public class SuperUser : ApplicationUser 

I am using NHibernate 3.3 and I want to create mappings for this inheritance

 public abstract class ModelEntityMap<TEntity> : ClassMapping<TEntity> where TEntity : ModelEntity {...} public class AuditableEntityMap<TEntity> : ModelEntityMap<TEntity> where TEntity : AuditableEntity { ...} public class ApplicationUserMap : AuditableEntityMap<ApplicationUser> {...} public class SuperUserMap : JoinedSubclassMapping<SuperUser>{...} 

When the application starts and tries to tune the database, the following exception occurs: Ambiguous mapping for SuperUser Several root objects BaseEntity / ApplicationUser were found

Possible solutions β€”Merge the mapping of the Entity root in that it represents the real root in the hierarchy β€” Insert an IModelInspector with logic to detect the real root object.

I used Fluent nhibernate with the same inheritance and worked great with SuperUserMap defined as public class SuperUserMap: SubClassMap {...}

I'm new to displaying Nhibernate code and quite confusing !!!

+4
source share
3 answers

I believe that there are two ways to solve this problem: a) Using the discriminator concept, which identifies the type of the stored class and, therefore, the correct object is retrieved from the database, in this case your class is mapped to a table that has all the columns plus the discriminator columns . Not sure how this works with multi-level inheritance, but this is what you can do with Google.

b) take a look at this post about how it deals with inheritance: http://fabiomaulo.blogspot.co.nz/2011/04/nhibernate-32-mapping-by-code_13.html you might have an idea to solve your a problem.

0
source

You can influence the decision whether an entity is a root entity by overriding the IsRootEntity logic of the model matching you use to create the mappings.

Here is an example that defines the default behavior of NHibernate by default:

  var modelMapper = new ConventionModelMapper(); modelMapper.IsRootEntity((type, declared) => { if (declared) return true; // Type has already been declared as root entity return type.IsClass && typeof(object) == type.BaseType && modelMapper.ModelInspector.IsEntity(type); }); 

You will have to tweak this solution logic to exclude the BaseEntity class as a possible root object.

0
source

I had this error with NHibernate 4.1.1 (May 2017), so I reply as I solved it for future reference

In my case, I copied the existing mapping of the inheriting class and forgot to change the parent mapping class to ClassMapping and encountered the same error

In other words, in your mapping class, check the parent class, make sure it is ClassMapping or JoinedSubclassMapping , if it is a child class

0
source

Source: https://habr.com/ru/post/1413205/


All Articles