@MappedSuperclass

You can use the @MappedSuperclass annotation or <mapped-superclass> XML element to define mappings for an abstract or non-persistent superclass, that are inherited by its subclass entities. A mapped superclass is a special type of class that is not persistent itself, but has subclasses that are persistent. A mapped superclass is useful for defined a common persistence superclass that defines common behavior across a set of classes, such as an id or version attribute. A mapped superclass should normally be abstract but is not required to be, but cannot have any persistent instances.

Mapped superclasses cannot define a table, but can define mapping for its attributes and other common persistence behavior. Entities cannot have relatinoships to mapped superclasses, and mapped superclasses cannot be queried.

For more information, see Section 11.1.34 "MappedSuperclass Annotation" in the JPA Specification.

@AttributeOverride

You can use the @AttributeOverride and @AttributeOverrides annotations, or <attribute-override> XML element to override the column for a basic attribute in a mapped superclass. This allows for the column name to be different in each subclass.

@AttributeOverride Attributes

Attribute

Description

Default

Required?

name

The name of the attribute.

Yes

column

The column in the subclass table.

column defined in mapped superclass

No

For more information, see Section 11.1.4 "AttributeOverride Annotation" in the JPA Specification.

@AssociationOverride

You can use the @AssociationOverride and @AssociationOverrides annotations, or <association-override> XML element to override the join column or join table for a relationship attribute in a mapped superclass. This allows for the join column name or join table to be different in each subclass.

@AssociationOverrideAttributes

Attribute

Description

Default

Required?

name

The name of the attribute.

Yes

joinColumn

The join column in the subclass table.

join column defined in mapped superclass

No

joinTable

The join table for the subclass.

join table defined in mapped superclass

No

For more information, see Section 11.1.2 "AssociationOverride Annotation" in the JPA Specification.

The following example shows usages of a generic persistence mapped superclass.