Skip to content

Latest commit

 

History

History
82 lines (59 loc) · 1.43 KB

05-JPA-Inheritance.md

File metadata and controls

82 lines (59 loc) · 1.43 KB

Inheritance

@MappedSuperclass

Strategy Single Table

Case :

erDiagram
    TB_PAYMENT {
        integer order_id PK,FK
        varchar payment_type
        varchar card_number
        varchar bar_code
        enum status
    }
Loading

Super class or abstract class configuration

import jakarta.persistence.DiscriminatorColumn;
import jakarta.persistence.DiscriminatorType;
import jakarta.persistence.Inheritance;

@Entity(name = "tb_payment")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "payment_type", discriminatorType = DiscriminatorType.STRING)
public abstract class Payment extends BaseEntityInteger {

    @MapsId
    @OneToOne(optional = false)
    @JoinColumn(name = "order_id")
    private Order order;

    @Enumerated(EnumType.STRING)
    private PaymentStatus status;
}

Concrete classes configuration

import jakarta.persistence.DiscriminatorValue;

@Entity
@DiscriminatorValue("card")
public class CardPayment extends Payment {

    @Column(name = "card_number")
    private String cardNumber;

}
import jakarta.persistence.DiscriminatorValue;

@Entity
@DiscriminatorValue("bank_slip")
public class BankSlipPayment extends Payment {

    @Column(name = "bar_code")
    private String barCode;

}

Strategy Table Per Class

erDiagram
    TB_CARD_PAYMENT {
        
    }
    TB_BANK_SLIP_PAYMENT {
        
    }    
Loading

Strategy Joined Table