@DiscriminatorFormular - Modeling Single Table Inheritance Without a Discriminator

preview_player
Показать описание
Inheritance is one of the key concepts of all object-oriented programming languages. And Java makes there no difference. All developers are familiar with this concept and expect to use it in all parts of their code. That, of course, also includes the persistence layer and the entity model. But the concept of inheritance doesn’t exist in relational table models. JPA and Hibernate bridge that gap by providing different inheritance mapping strategies that map the entities to one or more database tables.

The InheritanceType.SINGLE_TABLE is the default strategy and provides the best performance. It maps all entities of the inheritance hierarchy and their attributes to the same database table. Based on this mapping, your persistence provider can generate simple and efficient queries to fetch a specific subclass or all classes of the inheritance hierarchy.
Using this strategy introduces a technical requirement. For each database record, Hibernate needs to identify the subclass to which it has to map it. The default mapping uses a discriminator column, which contains a class-specific identifier. In most cases, that’s the simple name of the entity class.

But what do you do if you’re working with an existing table model that doesn’t contain such a column and that you’re not allowed to change? The JPA standard doesn’t provide a solution for this. It can’t use InheritanceType.SINGLE_TABLE without a discriminator column. But Hibernate does, if you can provide an SQL snippet that returns this information.

Links mentioned in the video:

Like my channel? Subscribe!

Want to connect with me?
Рекомендации по теме
Комментарии
Автор

Thank you so much sir. I like the way you describe things.

pathummadhusanka