Jpa joint primary key solves duplicate data problem and ${} problem

A single id does not satisfy the business usage scenario, new error reports primary key conflicts, and duplicate data and ${~} problems are found when querying data and printing, use jpa joint primary key to fix the problem

Table of contents

1. New joint primary key configuration class

2. The entity class configures the joint primary key


1. New joint primary key configuration class

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
public class EisaPriKey implements Serializable {

    private String key1;

    private String key2;
}

2. The entity class configures the joint primary key


import hk.org.ha.ris.primary.key.system.StReportTemplatePK;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.io.Serializable;

@Data
@Entity
//联合主键配置类
@IdClass(EisaPriKey.class)
@Table(name = "eisa")
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class eisa implements Serializable {
    @Id//这个注解很重要,是联合主键其中的一个
    @Column(name = "key1", nullable = false,unique = false)
    private String key1;

    @Id//这个注解也很重要,是联合主键的另外一个
    @Column(name = "key2", nullable = false)
    private String key2;

    @Column(name = "path", nullable = false)
    private String name;

}

Note that there can be two @Id annotations here,

Guess you like

Origin blog.csdn.net/qq_44691484/article/details/129918201