今天遇到個有意思的問題:findAll查出的數據居然很數據庫裏存儲的數據不同java
原本數據庫裏有全國全部省份的數據可是查出來只有河南和浙江的數據條數同樣,數據大量重複spring
通過分析是Entity的ID字段在數據庫中並非惟一的,並且重複的不少,後來改爲聯合主鍵就正常了。數據庫
選ID的時候必定要確認是不是惟一的,由於JPA底層會根據這個ID判斷倆個Entity是否是同樣的。ide
如下是配置聯合主鍵的代碼:this
/** * @Description: * @Author: tianlang * @Email: tianlangstudio@aliyun.com * @Date: 20-5-6 下午6:19 */package email.tianlangstudio.aliyun.com.datav.model;import javax.persistence.*;import java.math.BigDecimal;/** * 員工地域信息表 * 包含省、市、員工個數信息 * SELECT gsdm as 單位, nd as 年份, province as 省, city as 市, SUM(rgcb) AS 金額, SUM(sl) AS 數量 FROM map GROUP BY gsdm,nd,province,city * ***/@Entity@Table(name = "map")@IdClass(EmployeeRegionInfoKey.class)public class EmployeeRegionInfo {/* * 公司代碼 * **/@Id@Column(name = "gsdm")private String companyCode;/** * 年份 * **/@Id@Column(name = "nd")private Integer year;/* * 省名稱 * **/@Id@Column(name = "province")private String province;/** * 市名稱 * **/@Id@Column(name = "city")private String city;/** * 金額 * **/@Column(name = "rgcb")private BigDecimal moneyAmount;/** * 數量 * **/@Column(name = "sl")private Integer employeeAmount;public String getCity() {return city;}public void setCity(String city) {this.city = city;}public String getCompanyCode() {return companyCode;}public void setCompanyCode(String companyCode) {this.companyCode = companyCode;}public Integer getYear() {return year;}public void setYear(Integer year) {this.year = year;}public String getProvince() {return province;}public void setProvince(String province) {this.province = province;}public BigDecimal getMoneyAmount() {return moneyAmount;}public void setMoneyAmount(BigDecimal moneyAmount) {this.moneyAmount = moneyAmount;}public Integer getEmployeeAmount() {return employeeAmount;}public void setEmployeeAmount(Integer employeeAmount) {this.employeeAmount = employeeAmount;}}
/** * @Description: * @Author: tianlang * @Email: tianlangstudio@aliyun.com * @Date: 20-5-7 下午1:47 */package email.tianlangstudio.aliyun.com.datav.model;import org.springframework.context.annotation.Primary;import javax.persistence.Column;import javax.persistence.Embeddable;import java.io.Serializable;import java.math.BigDecimal;//@Embeddable//@Primarypublic class EmployeeRegionInfoKey implements Serializable {/* * 公司代碼 * **///@Column(name = "gsdm")private String companyCode;/** * 年份 * **///@Column(name = "nd")private Integer year;/* * 省名稱 * **///@Column(name = "province")private String province;/** * 市名稱 * **///@Column(name = "city")private String city;public String getCompanyCode() {if(companyCode == null) {companyCode = "";}return companyCode;}public void setCompanyCode(String companyCode) {this.companyCode = companyCode;}public Integer getYear() {return year;}public void setYear(Integer year) {this.year = year;}public String getProvince() {if(province == null) {province = "";}return province;}public void setProvince(String province) {this.province = province;}public String getCity() {if(city == null) {city = "";}return city;}public void setCity(String city) {this.city = city;}@Overridepublic boolean equals(Object obj) {EmployeeRegionInfoKey other = (EmployeeRegionInfoKey)obj;return other.getYear() == this.getYear() &&other.getCompanyCode().equals(this.getCompanyCode()) &&other.getProvince().equals(this.getProvince()) &&other.getCity().equals(this.getCity());}@Overridepublic int hashCode() {return (this.getCity() + this.getProvince() + this.getYear() + this.getCompanyCode()).hashCode();}}