mysql - 使用 between 和 Instant 的 JPA 查询不起作用
问题描述
我正在尝试进行查询以检索在两个日期之间创建的一些数据(表示为即时)。
下面是我正在使用的实体的摘录:
@Entity
public class HistoricalData {
@Id
@GeneratedValue
private Long id;
@Column
private String name;
@CreationTimestamp
private Instant timestamp;
@Column
private Double price;
}
以及我为检索两个 Instant 之间的数据而编写的查询;
@Query("select h from HistoricalData h where h.timestamp between :timestampStart and :timestampEnd and upper(name) = upper(:name)")
List<HistoricalData> findHistoricalDataBetween(@NonNull Instant timestampStart, @NonNull Instant timestampEnd, @NonNull String name);
这会产生这个 SQL 查询:
select historical0_.id as id1_5_, historical0_.price as price2_5_, historical0_.timestamp as timestam3_5_ from historical_data historical0_ where (historical0_.timestamp between ? and ?) and upper(historical0_.name)=upper(?)
我也写了“hibernate JPA”查询只是为了尝试但没有成功:
List<HistoricalData> findHistoricalDataByTimestampAfterAndTimestampBeforeAndName(@NonNull Instant timestampStart, @NonNull Instant timestampEnd, @NonNull String name);
请记住,上述所有查询都能正确编译并且不会抛出任何异常,它们只是从数据库中检索不到任何内容
我使用的数据库是 MariaDB 的最新版本,连接器版本是 2.7.2 我使用的 SpringBoot 版本是 2.5.3
这是表定义中的 DDL(从 Hibernate 自动生成):
create table historical_data
(
id bigint not null primary key,
price double null,
timestamp datetime not null,
name varchar not null
);
这是时间戳在数据库中的样子:
即使这两个 Instant 之间的记录存在于数据库中,我仍然没有从查询中得到任何结果。
解决方案
看起来原因是时区。MySQL 驱动程序使用不正确的时区转换,使用默认本地时区代替连接时区(反之亦然)。
只需在 MySQL 驱动程序中调试此查询即可获得乐趣并弄清楚会发生什么。
您可以将参数添加到数据库 URL 以查看为 prepare 语句传递的实际值
jdbc:mysql://<DATABASE_URL>?logger=com.mysql.cj.log.Slf4JLogger&profileSQL=true
推荐阅读
- spring-security - 使用 Spring Security 实现 OAuth2 隐式授权
- c# - 评价 ajaxtoolkit - OnChanged 事件不起作用
- javascript - 通过服务人员离线显示谷歌地图。
- python - 从 Python 列表中查找最近的 5 个日期
- wpf - 如何知道 UI 自动化客户端正在监听我的 WPF 应用程序
- javascript - 当发布正文内容为 JSON 但响应为 HTML 时发出 jquery ajax 发布请求
- angular - Auth0 错误:访问被拒绝;error_description=指纹无效
- vuejs2 - 在 vue-test-utils 中测试鼠标悬停事件
- spring - 使用 Spring 声明式缓存进行集成测试
- python - 解码python二进制字符串但不确保ascii符号