Inheritance relationships

Support of inheritance relationships

The Hibernate Designer module supports inheritance relationships between persistent classes.  The sections which follow deal with four inheritance mapping strategies supported by Hibernate:

  • one table per class hierarchy
  • one table per child class and its variant using a discriminator
  • one table per concrete class
  • one table per concrete class using implicit polymorphism

The type of mapping applied to a given inheritance link can be configured in the Hibernate tab on the inheritance link in question.

One table per class hierarchy

Using this strategy, the set of persistent classes in the inheritance hierarchy is mapped to a single table of the SQL model.

Constraints at persistence model level:

  • Only the parent persistent class declares an identifier. Child classes must not have one.
  • All the properties of these classes cannot have Not Null constraints.
  • The Discriminator Column, Discriminator Column Type and Discriminator Value properties must be defined at parent class level.

Constraints at SQL model level:

  • The SQL table must have a field for each of the properties declared by the persistent classes in the inheritance hierarchy.
  • The SQL table must have an extra field for the discriminator.
  • The fields of the SQL table cannot have the Not Null constraint.

One table per child class

Using this strategy, each of the persistent classes in the inheritance hierarchy is mapped to a table of the SQL model.

Constraints at persistence model level:

  • Only the parent persistent class declares an identifier. Child classes must not have one.

Constraints at SQL model level:

  • The tables of child classes have a primary key associated with the parent class table (the relational model is a one-to-one association).

The Hibernate Designer supports a variant of this strategy: one table per child class using a discriminator, which leads to the following constraints:

  • The Discriminator Column, Discriminator Column Type and Discriminator Value properties must be defined at parent class level.
  • The SQL table corresponding to the parent persistent class must have a column for the discriminator.

One table per concrete class

Using this strategy, each of the non-abstract persistent classes of the inheritance hierarchy is mapped to a table of the SQL model.

Constraints at persistence model level:

  • Only the parent persistent class declares an identifier. Child classes must not have one.

Constraints at SQL model level:

  • Each SQL table defines columns for all the properties of the class, including inherited properties.
  • The name of the column corresponding to Property must be the same for all child classes.

One table per concrete class using implicit polymorphism

This is a variation on the previous strategy. Each of the non-abstract persistent classes of the inheritance hierarchy is mapped to a table of the SQL model.

Constraints at persistence model level:

  • All the persistent classes of the inheritance hierarchy must declare the same identifier.

Constraints at SQL model level:

  • Each SQL table defines columns for all the properties of the class, including inherited properties.
  • The name of the column corresponding to Property must be the same for all the child classes.