@MappedSuperclass marks a class whose persistent fields and mappings are inherited by its entity subclasses, but which is not an entity itself. It has no table, cannot be queried or used as a target of associations, and exists only to share columns such as id, createdAt or version across entities without repeating them.
In current Jakarta Persistence (Jakarta EE 9 and later, including Spring Boot 3 and 4), the annotation lives in jakarta.persistence. Older code and Spring Boot 2 use the javax.persistence package with the same behaviour.
A class annotated with @MappedSuperclass contributes its mapped state to every entity that extends it. Each concrete entity gets its own table that contains the inherited columns plus its own.
Key properties:
FROM clauses or EntityManager.find.@Column, @Basic, @Temporal and similar annotations.@Id) and version (@Version) attributes. This is the most common use.@ManyToOne or @OneToMany association whose target type is the mapped superclass, because it is not an entity.The following base class provides an identifier and creation and update timestamps. Using java.time types is the modern choice over java.util.Date.
Lifecycle callbacks such as @PrePersist and @PreUpdate declared on a mapped superclass are inherited by all subclasses, so the timestamps are maintained in one place.
Each concrete entity extends the base class and adds its own fields.
Each public class goes in its own file in a real project. The generated schema looks like this:
The exact column types depend on your database and Hibernate version. There is no base_entity table.
A subclass can change an inherited column mapping with @AttributeOverride, for example to rename the id column.
| Aspect | @MappedSuperclass | @Inheritance on an entity |
|---|---|---|
| Is the parent an entity | No | Yes |
| Polymorphic queries on the parent | Not possible | Possible |
| Associations pointing to the parent | Not allowed | Allowed |
| Tables | One per concrete subclass, no parent table | Single table, joined, or table per class |
| Typical use | Shared columns and behaviour | A real domain hierarchy |
Choose @MappedSuperclass when you only want to reuse fields. Choose entity inheritance when you need to query a Vehicle and receive Car and Truck instances.
@Entity and @MappedSuperclass. A class is one or the other.SELECT b FROM BaseEntity b. That fails because the class is not an entity.BaseEntity instances.equals and hashCode carelessly on a generated id. Base them on a business key, or handle the null id before persist.javax.persistence imports into a Jakarta Persistence 3 project, which fails at compile time or startup.Spring Data JPA offers an alternative for auditing: annotate the base class with @EntityListeners(AuditingEntityListener.class) and use @CreatedDate and @LastModifiedDate, then enable it with @EnableJpaAuditing.
Yes. Declaring @Id in the mapped superclass is the usual pattern, and every subclass inherits the primary key mapping. A subclass hierarchy must end up with exactly one identifier definition.
It can declare @ManyToOne and @OneToOne associations whose target is an entity, and the subclasses inherit them. What it cannot be is the target of an association from another entity.
No. Only concrete @Entity subclasses get tables, and each table includes the inherited columns.
Not with JPQL, because it is not an entity. If you need polymorphic queries, make the parent an @Entity with an @Inheritance strategy, or use a Spring Data repository on each concrete type.
@MappedSuperclass shares mapped fields, lifecycle callbacks and the primary key definition across entities without creating a parent table or entity type. Use it for id, version and audit timestamps, import from jakarta.persistence on current stacks, and switch to entity inheritance when you need polymorphic queries or associations to the parent. The behaviour is defined in the Jakarta Persistence specification.