Hibernate supports four inheritance strategies to enhance reusability in the entity layer:
jakarta.persistence.MappedSuperclass
Annotation: @MappedSuperclass
Description: A class-level annotation applied to an abstract or concrete superclass. Hibernate does not generate a table for this class.
Purpose: Defines common fields that can be inherited by other entities.
Common Members:
Long id
with @Id
).@CreationTimestamp
for entity creation tracking.@UpdateTimestamp
for entity update tracking.@Version
for optimistic locking (e.g., private int myVersion
).Example:
@MappedSuperclass
public abstract class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@CreationTimestamp
private LocalDateTime createdAt;
@UpdateTimestamp
private LocalDateTime updatedAt;
@Version
private int myVersion;
// Getters and Setters
}
Review Note: The MappedSuperclass
section is complete, covering the annotation, its purpose, and typical fields. No additional inheritance strategies (Single Table, Joined, Table per Class) were detailed in the input, so I’ll focus on MappedSuperclass
as provided. If you need details on the other strategies, let me know.
Associations represent a weaker form of association (aggregation) since entities have standalone lifecycles and separate database identities (tables with primary keys).
Restaurant
(one) and FoodItem
(many).