Spring JPA使用findAll方法查出的数据跟数据库里存储的不一样

2024-04-04 00:38

本文主要是介绍Spring JPA使用findAll方法查出的数据跟数据库里存储的不一样,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

今天遇到个有意思的问题:findAll查出的数据竟然很数据库里存储的数据不一样

本来数据库里有全国所有省份的数据但是查出来只有河南和浙江的数据条数一样,数据大量重复

经过分析是Entity的ID字段在数据库中并不是唯一的,而且重复的很多,后来改成联合主键就正常了。

选ID的时候一定要确认是否是唯一的,因为JPA底层会根据这个ID判断俩个Entity是不是一样的。

以下是配置联合主键的代码:

/*** @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
//@Primary
public 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();}
}

 

这篇关于Spring JPA使用findAll方法查出的数据跟数据库里存储的不一样的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/874392

相关文章

MySQL查看表的历史SQL的几种实现方法

《MySQL查看表的历史SQL的几种实现方法》:本文主要介绍多种查看MySQL表历史SQL的方法,包括通用查询日志、慢查询日志、performance_schema、binlog、第三方工具等,并... 目录mysql 查看某张表的历史SQL1.查看MySQL通用查询日志(需提前开启)2.查看慢查询日志3.

MySQL底层文件的查看和修改方法

《MySQL底层文件的查看和修改方法》MySQL底层文件分为文本类(可安全查看/修改)和二进制类(禁止手动操作),以下按「查看方法、修改方法、风险管控三部分详细说明,所有操作均以Linux环境为例,需... 目录引言一、mysql 底层文件的查看方法1. 先定位核心文件路径(基础前提)2. 文本类文件(可直

Java实现字符串大小写转换的常用方法

《Java实现字符串大小写转换的常用方法》在Java中,字符串大小写转换是文本处理的核心操作之一,Java提供了多种灵活的方式来实现大小写转换,适用于不同场景和需求,本文将全面解析大小写转换的各种方法... 目录前言核心转换方法1.String类的基础方法2. 考虑区域设置的转换3. 字符级别的转换高级转换

使用Python将PDF表格自动提取并写入Word文档表格

《使用Python将PDF表格自动提取并写入Word文档表格》在实际办公与数据处理场景中,PDF文件里的表格往往无法直接复制到Word中,本文将介绍如何使用Python从PDF文件中提取表格数据,并将... 目录引言1. 加载 PDF 文件并准备 Word 文档2. 提取 PDF 表格并创建 Word 表格

使用Python实现局域网远程监控电脑屏幕的方法

《使用Python实现局域网远程监控电脑屏幕的方法》文章介绍了两种使用Python在局域网内实现远程监控电脑屏幕的方法,方法一使用mss和socket,方法二使用PyAutoGUI和Flask,每种方... 目录方法一:使用mss和socket实现屏幕共享服务端(被监控端)客户端(监控端)方法二:使用PyA

Python使用Matplotlib和Seaborn绘制常用图表的技巧

《Python使用Matplotlib和Seaborn绘制常用图表的技巧》Python作为数据科学领域的明星语言,拥有强大且丰富的可视化库,其中最著名的莫过于Matplotlib和Seaborn,本篇... 目录1. 引言:数据可视化的力量2. 前置知识与环境准备2.1. 必备知识2.2. 安装所需库2.3

MySQL数据目录迁移的完整过程

《MySQL数据目录迁移的完整过程》文章详细介绍了将MySQL数据目录迁移到新硬盘的整个过程,包括新硬盘挂载、创建新的数据目录、迁移数据(推荐使用两遍rsync方案)、修改MySQL配置文件和重启验证... 目录1,新硬盘挂载(如果有的话)2,创建新的 mysql 数据目录3,迁移 MySQL 数据(推荐两

SpringBoot简单整合ElasticSearch实践

《SpringBoot简单整合ElasticSearch实践》Elasticsearch支持结构化和非结构化数据检索,通过索引创建和倒排索引文档,提高搜索效率,它基于Lucene封装,分为索引库、类型... 目录一:ElasticSearch支持对结构化和非结构化的数据进行检索二:ES的核心概念Index:

Python数据验证神器Pydantic库的使用和实践中的避坑指南

《Python数据验证神器Pydantic库的使用和实践中的避坑指南》Pydantic是一个用于数据验证和设置的库,可以显著简化API接口开发,文章通过一个实际案例,展示了Pydantic如何在生产环... 目录1️⃣ 崩溃时刻:当你的API接口又双叒崩了!2️⃣ 神兵天降:3行代码解决验证难题3️⃣ 深度

Linux内核定时器使用及说明

《Linux内核定时器使用及说明》文章详细介绍了Linux内核定时器的特性、核心数据结构、时间相关转换函数以及操作API,通过示例展示了如何编写和使用定时器,包括按键消抖的应用... 目录1.linux内核定时器特征2.Linux内核定时器核心数据结构3.Linux内核时间相关转换函数4.Linux内核定时