Nov 3, 2018

JPA Annotations – @MappedSuperclass

@MappedSuperclass is used to annotate a class whose instance won't be persisted or participate in any persisting operation, while its subclasses will do. A class annotated with an @MappedSuperclass, even if it has no abstract non-access modifier, will not be considered in any persisting operation. However, all subclasses that inherit from the superclass and are not marked as @MappedSuperclass will participate in the persisting operations including the fields inherit from the superclass. The main goal of @MappedSuperclass is to share the mapping information with the sub classes.

An entity may inherit from a superclass annotated with @Inheritance that provides a persistent entity state and mapping information. Different from @Inheritance, the @MappedSuperclass doesn’t involve in any persistent activity and it doesn’t involve in any operations that managed by the EntityManager. The main purpose of using a @MappedSuperclass is to define a common states and mapping information that could be shared by multiple entity classes.

A @MappedSuperclass annotated class is not persisted itself, but the subclasses are persistent. So, @MappedSuperclass is used for an abstract class or a non-persistent class.

Some of the characteristics of @MappedSuperclass are:
  • Normally class is abstract, but it never be a persistent instance
  • Mapped super classes can not define a table, but can define mapping for attributes.
  • This annotation doesn’t have any attributes.