Hibernate @MappedSuperclass for Inheritance
Hibernate is its support for inheritance, and the @MappedSuperclass
annotation plays a central role in this. In this article, we will explore how to use @MappedSuperclass
to create an inheritable entity structure, using a simple account model as an example.
Hi, this is Paul, and welcome to the Hibernate guide. Today we will discuss how to use@MappedSuperclass
for implementing inheritance in Hibernate Entities and avoiding code duplication.
Definition of @MappedSuperclass
@MappedSuperclass
indicates that a class is a superclass and is not associated with a specific database table, but its fields (or properties) can be inherited by child entity classes that are associated with tables. This helps avoid code duplication and ensures a cleaner object model structure.
Usage Example
Let’s look at an example of using @MappedSuperclass
with a base and child class associated with the accounts
table in the database.
CREATE TABLE accounts
(
id BIGSERIAL PRIMARY KEY,
created_at TIMESTAMP,
balance NUMERIC(10, 2),
is_active BOOLEAN
);
Creating the Base Class
First, we create a base class BaseEntity
, which contains common fields such as id
and createdAt
. These fields will be inherited by all child entities.
import jakarta.persistence.*;
import lombok.*;
import lombok.experimental.SuperBuilder;
import java.time.LocalDateTime;
import java.util.Objects;
@Getter
@Setter
@ToString
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@MappedSuperclass
public abstract class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "created_at")
private LocalDateTime createdAt;
// equals and hashCode
}
Extending the Base Class
Next, we create an Account
class that extends BaseEntity
and is associated with the accounts
table. This class adds account-specific fields such as balance
and isActive
.
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.*;
import lombok.experimental.SuperBuilder;
import java.math.BigDecimal;
import java.util.Objects;
@Entity
@Getter
@Setter
@ToString
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "accounts")
public class Account extends BaseEntity {
@Column(name = "balance", precision = 10, scale = 2)
private BigDecimal balance;
@Column(name = "is_active")
private Boolean isActive;
// equals and hashCode
}
Working with the Repository
Repository implementation as usual nothing special.
@Repository
@Transactional
public interface AccountRepository extends JpaRepository<Account, Long> {}
Benefits of Using @MappedSuperclass
- Avoiding Code Duplication: Common fields and methods of the base class are automatically inherited by all child classes.
- Flexibility in Data Modeling:
@MappedSuperclass
allows for more flexible and scalable data models. - Simplified Maintenance: Centralizing common fields simplifies their update and maintenance.
Conclusion
The @MappedSuperclass
annotation in Hibernate provides a powerful tool for inheritance in object-oriented data models. It enables developers to create a clean, modular, and easily maintainable application architecture, simplifying database interactions and reducing the amount of duplicated code.
Thank you for reading until the end. Before you go:
- Please consider clapping and following the writer! 👏
- Follow us on Twitter(X), LinkedIn