Search code examples
nhibernatenhibernate-mappingtable-per-hierarchy

Table-per-hierarchy mapping when subclasses have not-nullable fields?


I have a class hierarchy with an abstract base class and 4-5 subclasses. I want to use NHibernate's table-per-class-hierarchy mapping method to map these to a single database table to reduce the number of joins my queries generate.

However, one of those subclasses has a not-null property which the other subclasses lack. This means that whenever I try to save an entity of one of those other subtypes, I get a SQL error saying that the missing field cannot be null.

Does this mean that table-per-hierarchy is simply incompatible with my domain model, or is there something else I need to do?


Solution

  • What do you expect that field to contain for the classes that don't have that property?

    Since it's probably NULL, you should just make it nullable, but validate it in your model/business logic.