In this blog we ‘ll learn about JPA @MappedSuperClass annotation for Inheritance. This @MappedSuperClass is one of the useful annotation of JPA that use most frequently in project. As the name suggest we use this annotation to inherit super class property in the child class. For example you often seen that whenever we create any entity we define the Id and we also use JPA auditing annotations @CreatedDate and @LastModifiedDate for maintain the audit details of our entity table.
So one option is we create all these common properties in each and every entity class but it make our code repeatable that’s not a good practice as we are developer and believe on clean coding. So we opt the second way we create a class that contains these fields and extend this class in other entity. Using this way our code is clean and no code repetition is there.
If you don’t want to go inside the details and need only code you can skip the details and directly checkout the code from Github.
Let’s see what we are going to learn in the blog
- What is @MappedSuperClass annotation and why we use this.
- Difference between @MappedSuperClass and other JPA inheritance annotations.
What is @MappedSuperClass annotation and why we use this
@MappedSuperClass annotation is use for inheritance in JPA. So whatever property is common in our entity we create a abstract class annotated with @MappedSuperClass and define that properties in this class. And in all the entities we just extends this class. Lets’ understand it by an example.
Suppose i want to create a user_detail table that contains details about the user. But there are some property that is common in all entity like for every entity we create the Id field. And we also want to track when a column entry is created and when this specific column has been updated. These three fields are very much common in each and every entity class. So instead of defining in each and every entity we create the base class with these three fields and extends this class in each entity where we need these three columns.
Lets’ see the code part so you ‘ll understand it better.
1- Create a base entity class that contains the common fields.
BaseEntity.java
package com.javadream.MappedSuperClassJPAExample.entity;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import java.time.LocalDateTime;
@Getter
@Setter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@CreatedDate
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime updatedAt;
}Code language: CSS (css)
You can see in above abstract class we have created three fields (id, createdAt, updatedAt). If you worked on JPA you know that these three property is used in each and every entity. So instead of repeating this fields in each and every entity we created this class and annotated it with @MappedSuperclass annotation.
Now see how can we get these three property in our entity class. So we ‘ll create a entity class and extend the BaseEntity class there.
User.java
package com.javadream.MappedSuperClassJPAExample.entity;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Entity
@Table(name = "user_detail")
public class User extends BaseEntity{
private String name;
private String emailId;
private String mobileNumber;
}Code language: JavaScript (javascript)
See in above code we just define name, emailId, mobileNumber only in our entity class. We haven’t define id, createdAt, updatedAt instead we are just extending the BaseEntity class that contains these three property.
So if you run your project you ‘ll see that user_detail table ‘ll be created and BaseEntity properties also come in this table. You table ‘ll look like below.

Please ignore these @CreatedDate, @LastModifiedDate, @EntityListeners(AuditingEntityListener.class) annotation as of now as these are the JPA auditing annotations. These annotation is use to track the details like when a column is created and when it’s updated. we don’t need to provide these details these ‘ll be automatically handled by JPA.
Make Sure if you want to use these JPA auditing annotations then your main class should be annotated with @EnableJpaAuditing. If you don’t use these annotation on main class your JPA auditing ‘ll not work.
MappedSuperClassJpaExampleApplication .java
package com.javadream.MappedSuperClassJPAExample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@SpringBootApplication
@EnableJpaAuditing
public class MappedSuperClassJpaExampleApplication {
public static void main(String[] args) {
SpringApplication.run(MappedSuperClassJpaExampleApplication.class, args);
}
}Code language: JavaScript (javascript)
Let’s create a controller to expose a endpoint for creating the user and see how these jpa auditing annotation automatically update.
UserController .java
package com.javadream.MappedSuperClassJPAExample.controller;
import com.javadream.MappedSuperClassJPAExample.entity.User;
import com.javadream.MappedSuperClassJPAExample.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@AllArgsConstructor
public class UserController {
private final UserService userService;
@PostMapping("/createUser")
public String createUser(@RequestBody User user) {
return userService.createUser(user);
}
}
Code language: CSS (css)
UserReposiotry.java
package com.javadream.MappedSuperClassJPAExample.repository;
import com.javadream.MappedSuperClassJPAExample.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserReposiotry extends JpaRepository<User, Long> {
}Code language: CSS (css)
UserService .java
package com.javadream.MappedSuperClassJPAExample.service;
import com.javadream.MappedSuperClassJPAExample.entity.User;
import com.javadream.MappedSuperClassJPAExample.repository.UserReposiotry;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@AllArgsConstructor
public class UserService {
private final UserReposiotry userReposiotry;
public String createUser(User user) {
userReposiotry.save(user);
return "User created successfully";
}
}Code language: JavaScript (javascript)
Now run your application and call the createUser endpoint. We just provide the user detail like name, emailId, mobileNumber. other fields that is annotated with @CreatedDate, @LastModifiedDate ‘ll update automatically.
Test application


You can see the details in database table. We just provide user details, Other fields with JPA auditing annotation update automatically. If you update this column you ‘ll see updatedat column value ‘ll update automatically. You can play with this just expose endpoint for updating this user detail and see the updatedat column value it ‘ll automatically update. I am leaving this exercise to you just try and put your finding in comment section of this blog.
Difference between @MappedSuperClass and other JPA inheritance annotations
Now you are wondering why we are using @MappedSuperClass annotation when we already have @Inheritance in JPA that is use for inheritance. So the answer is @MappedSuperClass annotation is use when we want to use the inheritance but don’t need the base table.
@MappedSuperClass annotation only purpose is to be use for inheritance, no table is created but with @inheritance table is created. So we can say that if you need to inherit the property and don’t need the base class table use @MappedSuperClass and if you need to inherit the property and also need the base class table use @Inheritance.
Hope you enjoy this blog. If you like this please share your feedback in comment section. Please share this blog with others and help us to grow.
If you need blog on any specific topic please provide detail in our comment section we ‘ll try to cover in our upcoming blog. Thanks