views:

313

answers:

1

Possibly a dumb question but I have a number of entities all inheriting from a base entity. The base entity does not have a table in the database. Each entity has its own table and the table definition is exactly the same. Extremely simplified example of the code is below.

public abstract class BaseEntity
{
    public virtual string someProperty {get; set;} 
}

public class Entity1 : BaseEntity{}

public class Entity2 : BaseEntity{}

public class CompletelyDifferentEntity
{
    public virtual IList<BaseEntity> {get; set;}
}

I created the mappings for the entities. In my other domain classes if I reference the concrete classes everything works fine but if I change my other classes to reference BaseEntity instead I get a mapping Exception because the BaseEntity is not mapped. Is this something where I should use a subclass discriminator? I guess I'm not seeing the correct way to do this if the base doesn't have an associated table and the subclasses don't have a specific column that is different between the table definitions.

+1  A: 

You have to use one of three available inheritance mappings strategies. From your description, you should consider using table-per-concrete-class mapping, or change your db scheme.

You can find more information about pros and cons of strategies here: https://www.hibernate.org/hib_docs/nhibernate/html/inheritance.html.

maciejkow
I will look into the table-per-concrete mapping a bit more. Unfortunately changing the db scheme will not work because there are several pre-existing systems that depend on the schema as it currently is.
Ryan Lanciaux

related questions