[ad_1]
@MappedSupperclass
is different than the @Inheritance
annotation.
@MappedSuperclass
tells the JPA provider to include the base class persistent properties as if they were declared by the child class extending the superclass annotated with @MappedSuperclass
.
However, the inheritance is only visible in the OOP world, since, from a database perspective, there’s no indication of the base class. Only the child class entity will have an associated mapped table.
The @Inheritance
annotation is meant to materialize the OOP inheritance model in the database table structure. More, you can query a base class annotated with @Inheritance
but you can’t do that for a base class annotated with @MappedSuperclass
.
Now, the reason why you’d want to use the @Inheritance
JPA annotation is to implement behavior-driven patterns like the Strategy Pattern.
On the other hand, @MappedSuperclass
is just a way to reuse both basic properties, associations, and even the entity @Id
using a common base class. Nevertheless, you can achieve almost the same goal using an @Embeddable
type. The only major difference is that you can’t reuse an @Id
definition with @Embeddable
, but you can do it with @MappedSuperclass
.
[ad_2]