Use of hibernate's @ EmbeddedId annotation

Keywords: Hibernate

I. Preface

When we design tables for projects, sometimes we need to use composite primary keys to identify uniqueness. If the project's persistence layer adopts hibernate framework, we can use @ EmbeddedId annotation to achieve this requirement.

2, Table design

CREATE TABLE `mkt_mas_cpcus` (
  `CR_ORG_NBR` decimal(3,0) NOT NULL DEFAULT '0' COMMENT 'Organization number',
  `CR_ACCT_NBR` decimal(16,0) NOT NULL DEFAULT '0' COMMENT 'Cardholder code',
  `CR_STATUS` decimal(1,0) DEFAULT NULL COMMENT 'state',
  `CR_SHORT_NAME` varchar(15) DEFAULT NULL COMMENT 'Full name',
  PRIMARY KEY (`CR_ORG_NBR`,`CR_ACCT_NBR`)
)

In this table, the organization No. CR? Org? NBR and cardholder code CR? Acct? NBR are used as composite primary keys to uniquely identify a piece of data.

3, Create a compound primary key class

@Embeddable
public class MasCpcusPK implements Serializable {
    private static final long serialVersionUID = 1L;

    private BigDecimal crAcctNbr; // Cardholder code
    private BigDecimal crOrgNbr; // Organization number

    public MasCpcusPK() {
    }  
    @Basic
    @Column(name="CR_ACCT_NBR")
    public BigDecimal getCrAcctNbr() {
        return this.crAcctNbr;
    }
    public void setCrAcctNbr(BigDecimal crAcctNbr) {
        this.crAcctNbr = crAcctNbr;
    }
    @Basic
    @Column(name="CR_ORG_NBR")
    public BigDecimal getCrOrgNbr() {
        return this.crOrgNbr;
    }
    public void setCrOrgNbr(BigDecimal crOrgNbr) {
        this.crOrgNbr = crOrgNbr;
    }
}

4, Create po class with hibernate annotation

Replace the two properties with the composite primary key, and add the @ EmbeddedId annotation to the get method.

@Entity
@Table(name = "mkt_mas_cpcus", schema = "market", catalog = "")
public class MktMasCpcus implements Serializable {

    private static final long serialVersionUID = -8509123963537411929L;

    private MasCpcusPK id; // Composite primary key

    private BigDecimal crStatus; // Customer status

    private String crShortName; // Full name

    @EmbeddedId
    public MasCpcusPK getId() {
        return id;
    }

    public void setId(MasCpcusPK id) {
        this.id = id;
    }
    @Basic
    @Column(name = "CR_STATUS")
    public BigDecimal getCrStatus() {
        return crStatus;
    }

    public void setCrStatus(BigDecimal crStatus) {
        this.crStatus = crStatus;
    }

    @Basic
    @Column(name = "CR_SHORT_NAME")
    public String getCrShortName() {
        return crShortName;
    }

    public void setCrShortName(String crShortName) {
        this.crShortName = crShortName;
    }
}

5, Create DAO layer

Since it is a composite primary key, the primary key in the generic must write this composite primary key, MasCpcusPK.

public interface MktMasCpcusDao extends JpaRepository<MktMasCpcus, MasCpcusPK> {

}

If you want to call the interface of Dao layer, when querying according to the id, the id will be passed to MasCpcusPK.

Posted by bass123 on Tue, 31 Dec 2019 22:30:03 -0800