首页 > 解决方案 > Hibernate 从 MySQL 检索 LocalDate 减去一天

问题描述

问题

我面临LocalDate从数据库中错误地获取的问题。

在这种情况下,我保存了一个日期,2020-06-04当我从数据库中检索它时,我得到了2020-06-03,所以减去一天。IntelliJ 的以下屏幕截图可视化了该问题:

IDE 截图

我启用了 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一切都像以前一样工作。

在我看来,结束问题是不合理的,因为原因是它是重复的。指向“重复”的链接包含日期以偏移量保存而不是检索的问题。因此,我建议重新打开这个问题,以便其他人能够从这一发现中受益!

标签: javamysqlspringhibernatespring-data-jpa

解决方案


推荐阅读