@MappedSuperclass JPA Example

This post demonstrates the usage of @MappedSuperclass JPA annotation with an example.

The JPA standard specification defines the @MappedSuperclass annotation to allow an entity to inherit properties from a base class.
From a database perspective, the @MappedSuperclass inheritance model is invisible since all the base class properties are simply copied to the database table mapped by the actual entity class.

Designates a class whose mapping information is applied to the entities that inherit from it. A mapped superclass has no separate table defined for it.

A class designated with the MappedSuperclass annotation can be mapped in the same way as an entity except that the mappings will apply only to its subclasses since no table exists for the mapped superclass itself. 

@MappedSuperclass Annotation Example 1

In the following domain model class hierarchy, a DebitAccount and a CreditAccount share the same Account base class.

@MappedSuperclass
public static class Account {

    @Id
    private Long id;

    private String owner;

    private BigDecimal balance;

    private BigDecimal interestRate;

    //Getters and setters are omitted for brevity

}

@Entity(name = "DebitAccount")
public static class DebitAccount extends Account {

    private BigDecimal overdraftFee;

    //Getters and setters are omitted for brevity

}

@Entity(name = "CreditAccount")
public static class CreditAccount extends Account {

    private BigDecimal creditLimit;

    //Getters and setters are omitted for brevity

}
CREATE TABLE DebitAccount (
    id BIGINT NOT NULL ,
    balance NUMERIC(19, 2) ,
    interestRate NUMERIC(19, 2) ,
    owner VARCHAR(255) ,
    overdraftFee NUMERIC(19, 2) ,
    PRIMARY KEY ( id )
)

CREATE TABLE CreditAccount (
    id BIGINT NOT NULL ,
    balance NUMERIC(19, 2) ,
    interestRate NUMERIC(19, 2) ,
    owner VARCHAR(255) ,
    creditLimit NUMERIC(19, 2) ,
    PRIMARY KEY ( id )
)

A class designated with the MappedSuperclass annotation whose mapping information is applied to the entities that inherit from it. A mapped superclass has no separate table defined for it as shown in the above DDL SQL script.

Check out the complete tutorial at https://www.javaguides.net/2018/11/hibernate-jpa-mappedsuperclass-inheritance-example.html.

@MappedSuperclass Annotation Example 2

Employee - Base Class

    @MappedSuperclass
    public class Employee {
    
        @Id protected Integer empId;
        @Version protected Integer version;
        @ManyToOne @JoinColumn(name="ADDR")
        protected Address address;
    
        public Integer getEmpId() { ... }
        public void setEmpId(Integer id) { ... }
        public Address getAddress() { ... }
        public void setAddress(Address addr) { ... }
    }

Let's create FullTimeEmployee and PartTimeEmployee entities to inherit properties from an Employee base class.

FullTimeEmployee.java

   // Default table is FTEMPLOYEE table
   @Entity
   public class FullTimeEmployee extends Employee {
   
       // Inherited empId field mapped to FTEMPLOYEE.EMPID
       // Inherited version field mapped to FTEMPLOYEE.VERSION
       // Inherited address field mapped to FTEMPLOYEE.ADDR fk
   
       // Defaults to FTEMPLOYEE.SALARY
       protected Integer salary;
   
       public FullTimeEmployee() {}
   
       public Integer getSalary() { ... }
   
       public void setSalary(Integer salary) { ... }
   }

PartTimeEmployee.java

    @Entity @Table(name="PT_EMP")
    @AssociationOverride(
        name="address", 
        joincolumns=@JoinColumn(name="ADDR_ID"))
    public class PartTimeEmployee extends Employee {
    
        // Inherited empId field mapped to PT_EMP.EMPID
        // Inherited version field mapped to PT_EMP.VERSION
        // address field mapping overridden to PT_EMP.ADDR_ID fk
        @Column(name="WAGE")
        protected Float hourlyWage;
    
        public PartTimeEmployee() {}
    
        public Float getHourlyWage() { ... }
        public void setHourlyWage(Float wage) { ... }
    }

References

https://www.javaguides.net/2018/11/hibernate-jpa-mappedsuperclass-inheritance-example.html.


Comments