Jpa联合主键
程序员文章站
2022-03-02 15:32:49
...
项目背景:SpringBoot+jpa+h2数据库。
本菜鸟接了个需求,在数据库里增加一些数据。sql语句发过来了,如下所示:
INSERT INTO st_report_template(hosp_cde, section, path, last_upd_usr, last_upd_dt, status_cde) VALUES('BH', 'ADDITIONAL_INFO', 'exam_report_additional_info.html', 'sa', '20140204 15:43:55', 'A');
INSERT INTO st_report_template(hosp_cde, section, path, last_upd_usr, last_upd_dt, status_cde) VALUES('BH', 'CONTENT', 'exam_report_content.html', 'sa', '20140204 15:43:55', 'A');
INSERT INTO st_report_template(hosp_cde, section, path, last_upd_usr, last_upd_dt, status_cde) VALUES('BH', 'FOOTER', 'exam_report_footer.html', 'sa', '20140204 15:43:55', 'A');
INSERT INTO st_report_template(hosp_cde, section, path, last_upd_usr, last_upd_dt, status_cde) VALUES('BH', 'LAYOUT', 'exam_report_overlay.html', 'sa', '20140204 15:43:55', 'A');
INSERT INTO st_report_template(hosp_cde, section, path, last_upd_usr, last_upd_dt, status_cde) VALUES('HCH', 'ADDITIONAL_INFO', 'exam_report_additional_info.html', 'syncdata', '20190122 12:05:38', 'A');
INSERT INTO st_report_template(hosp_cde, section, path, last_upd_usr, last_upd_dt, status_cde) VALUES('HCH', 'CONTENT', 'exam_report_content.html', 'syncdata', '20190122 12:05:38', 'A');
INSERT INTO st_report_template(hosp_cde, section, path, last_upd_usr, last_upd_dt, status_cde) VALUES('HCH', 'FOOTER', 'exam_report_footer.html', 'syncdata', '20190122 12:05:38', 'A');
INSERT INTO st_report_template(hosp_cde, section, path, last_upd_usr, last_upd_dt, status_cde) VALUES('HCH', 'LAYOUT', 'exam_report_overlay.html', 'syncdata', '20190122 12:05:38', 'A');
INSERT INTO st_report_template(hosp_cde, section, path, last_upd_usr, last_upd_dt, status_cde) VALUES('KH', 'ADDITIONAL_INFO', 'exam_report_additional_info.html', 'sa', '20140204 15:43:55', 'A');
INSERT INTO st_report_template(hosp_cde, section, path, last_upd_usr, last_upd_dt, status_cde) VALUES('KH', 'CONTENT', 'exam_report_content.html', 'sa', '20140204 15:43:55', 'A');
INSERT INTO st_report_template(hosp_cde, section, path, last_upd_usr, last_upd_dt, status_cde) VALUES('KH', 'FOOTER', 'exam_report_footer.html', 'sa', '20140204 15:43:55', 'A');
INSERT INTO st_report_template(hosp_cde, section, path, last_upd_usr, last_upd_dt, status_cde) VALUES('KH', 'LAYOUT', 'exam_report_overlay.html', 'sa', '20140204 15:43:55', 'A');
INSERT INTO st_report_template(hosp_cde, section, path, last_upd_usr, last_upd_dt, status_cde) VALUES('QEH', 'ADDITIONAL_INFO', 'exam_report_additional_info.html', 'sa', '20140204 15:43:55', 'A');
INSERT INTO st_report_template(hosp_cde, section, path, last_upd_usr, last_upd_dt, status_cde) VALUES('QEH', 'CONTENT', 'exam_report_content.html', 'sa', '20140204 15:43:55', 'A');
INSERT INTO st_report_template(hosp_cde, section, path, last_upd_usr, last_upd_dt, status_cde) VALUES('QEH', 'FOOTER', 'exam_report_footer.html', 'sa', '20140204 15:43:55', 'A');
INSERT INTO st_report_template(hosp_cde, section, path, last_upd_usr, last_upd_dt, status_cde) VALUES('QEH', 'LAYOUT', 'exam_report_overlay.html', 'sa', '20140204 15:43:55', 'A');
打眼一看,简单。cv一下,运行,报错:主键冲突。
再仔细看看,我把第一列当成主键,这么多重复的???!!!惊呆了,怎么办?
百度一番,使用jpa联合索引。
仔细看上面的sql语句:第一列很多重复的,但是第一列+第二列是不是就不重复了?怎么实现呢?上代码:
第一步,先写一个作为联合主键的实体类,把这两列放进来
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
public class StReportTemplatePK implements Serializable {
private String hospCde;
private String section;
}
第二步,写真正的实体类
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(StReportTemplatePK.class)
@Table(name = "st_report_template")
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class StReportTemplate implements Serializable {
@Id
@Column(name = "hosp_cde", nullable = false,unique = false)
private String hospCde;
@Id
@Column(name = "section", nullable = false)
private String section;
@Column(name = "path", nullable = false)
private String path;
@Column(name = "lastUpdUsr", nullable = false)
private String lastUpdUsr;
@Column(name = "lastUpdDt", nullable = false)
private String lastUpdDt;
@Column(name = "statusCde", nullable = false)
private String statusCde;
}
注意看,这里的@Id注解已经不止一个,就是包含的这两列。
第三步,写repository
@Repository
public interface StReportTemplateRepository extends JpaRepository<StReportTemplate, StReportTemplatePK>, JpaSpecificationExecutor<StReportTemplate> {
}
完成。
注意坑:
命名一定要写规范,项目越来越大的情况下,前缀名都相同,很容易导错,错的多了血压都上来了。
下一篇: JPA学习之联合主键