java - Hibernate 从 MySQL 检索 LocalDate 减去一天
问题描述
问题
我面临LocalDate
从数据库中错误地获取的问题。
在这种情况下,我保存了一个日期,2020-06-04
当我从数据库中检索它时,我得到了2020-06-03
,所以减去一天。IntelliJ 的以下屏幕截图可视化了该问题:
我启用了 Hibernate 的日志记录以验证它不是 Jackson 反序列化问题。日志声明日期已插入,2020-06-04
如下所示:
2020-05-21 14:55:55.587 INFO 16608 --- [ main] n.t.d.l.l.SLF4JQueryLoggingListener :
Name:dataSource, Connection:8, Time:1, Success:True
Type:Prepared, Batch:False, QuerySize:1, BatchSize:0
Query:["insert into voucher (created_at, version, application_object_id, application_type, code, max_usages, usages, valid_until, value, voucher_creator_id, voucher_type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"]
Params:[(2020-05-21 12:55:55.5468081,0,1,SPORTS_FACILITY,WINTER30,NULL(INTEGER),0,2020-06-04,30,NULL(BIGINT),RELATIVE)]
配置
春季启动2.3.0.RELEASE
休眠5.4.15.Final
MySQL 5.7
application.properties
:
- spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useLegacyDatetimeCode=false&zeroDateTimeBehavior=convertToNull
-spring.jpa.properties.hibernate.jdbc.time_zone=UTC
我也有设置 JVM 时间的类,如下所示:
@Configuration
@Slf4j
public class TimeZoneConfiguration {
@PostConstruct
void setDefaultTimeZone() {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
log.info("Spring boot application running in timezone UTC, now is: {} ", LocalDateTime.now());
}
}
该表是使用以下 SQL 脚本创建的:
create table voucher
(
id bigint not null auto_increment,
application_object_id bigint,
application_type varchar(40) not null,
code varchar(30) not null,
max_usages integer,
usages integer not null,
valid_until date not null,
value integer not null,
voucher_creator_id bigint,
voucher_type varchar(40) not null,
created_at datetime(6) not null,
version integer not null,
primary key (id),
foreign key (voucher_creator_id) references vendor (id)
) engine = InnoDB;
Voucher
实体如下所示(为简洁起见,省略了 Getters、Setters 等):
@Entity
public class Voucher {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
@GenericGenerator(name = "native", strategy = "native")
private Long id;
@Column(nullable = false, updatable = false)
@CreationTimestamp
private LocalDateTime createdAt;
@Version
private int version;
@Column(updatable = false)
private Long applicationObjectId;
@Column(nullable = false, updatable = false)
@Enumerated(EnumType.STRING)
private ApplicationType applicationType;
@Column(length = 30, nullable = false, updatable = false)
private String code;
@Column(updatable = false)
private Integer maxUsages;
@Column(nullable = false)
private int usages = 0;
@Column(nullable = false, updatable = false)
private LocalDate validUntil;
@Column(nullable = false, updatable = false)
private int value;
@JoinColumn(name = "voucher_creator_id", updatable = false)
@ManyToOne(fetch = FetchType.LAZY)
private Vendor voucherCreator;
@Column(nullable = false, updatable = false)
@Enumerated(EnumType.STRING)
private VoucherType voucherType;
}
编辑
我最近切换到8.0.20
了该mysql-connector-java
库的版本。切换回版本后,8.0.19
一切都像以前一样工作。
在我看来,结束问题是不合理的,因为原因是它是重复的。指向“重复”的链接包含日期以偏移量保存而不是检索的问题。因此,我建议重新打开这个问题,以便其他人能够从这一发现中受益!
解决方案
推荐阅读
- django - 如何限制用户访问某些特定的 url?
- c# - IComparable.CompareTo 正确实现考虑继承
- javascript - 根据第一个下拉框值预填充第二个下拉框
- visual-studio - Visual Studio 2017 中应用程序的自定义安装程序
- javascript - ES6 可以在 webpack.config.js 中原生使用吗?
- javascript - 在 Electron 中使用 AWS Cognito 的持久用户会话
- php - 使用 curl 下载视频的百分比
- python - 在 PyCharm 和 Python 3.6 中混淆 Mypy 类型检查
- r - 如何将随机森林模型从 R 导入 Android Studio?
- excel - Excel选择第一个值属于一个名称