如何将 TIMESTAMP 列映射到 ZonedDateTime JPA 实体属性?

How to map a TIMESTAMP column to a ZonedDateTime JPA entity property?(如何将 TIMESTAMP 列映射到 ZonedDateTime JPA 实体属性?)
本文介绍了如何将 TIMESTAMP 列映射到 ZonedDateTime JPA 实体属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring data jpama​​riadb 最新版本,以及 MariaDB 10.3.16

I'm using Spring data jpa and mariadb latest version, and MariaDB 10.3.16

+--- org.springframework.boot:spring-boot-starter-data-jpa -> 2.1.5.RELEASE
...
|    +--- org.springframework.boot:spring-boot-starter-jdbc:2.1.5.RELEASE
...
|    +--- org.hibernate:hibernate-core:5.3.10.Final

这是我的实体:

@Entity
@Data
@Table
@NoArgsConstructor
@AllArgsConstructor
public class Note {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Integer id;

    @Column
    private String gsn;

    @Column
    @Enumerated(EnumType.STRING)
    private NoteType type;

    @Column
    private String text;

    @Column
    private ZonedDateTime scheduleDt;

    @Column
    @CreationTimestamp
    private Instant createDt;

    @Column
    @UpdateTimestamp
    private ZonedDateTime updateDt;
}

当我持久化我的实体时,Hibernate 会尝试将 ZonedDateTime 成员保存为 DATETIME 列.但我想使用 TIMESTAMP 列而不是 DATETIME 列.

When I persist my entity, Hibernate tries to save ZonedDateTime member as DATETIME column. But I want to use TIMESTAMP column instead of DATETIME column.

这是创建 DDL,我从日志中看到的.

This is create DDL, what I see from log.

create table `note` (`id` integer not null, `create_dt` datetime,
    `gsn` varchar(255), `schedule_dt` datetime, `text` varchar(255),
    `type` varchar(255), `update_dt` datetime, primary key (`id`)) 
  engine=MyISAM

这里 create_dt, schedule_dt, update_dt 被创建为 datetime 列类型,这不是我想要的.(我也不喜欢 MyISAM).

Here create_dt, schedule_dt, update_dt is created as datetime column type, what is not I wanted. (I don't like MyISAM, too).

我该如何解决?

由于注释无法表达ddl而添加.

Added because comment cannot express ddl.

当我使用 columnDefinition 属性时,生成的 ddl 是 ...

When I use columnDefinition attribute, generated ddl is ...

create table `note` (`id` integer not null, `create_dt` datetime,
    `gsn` varchar(255), `schedule_dt` datetime, `text` varchar(255),
    `type` varchar(255), `update_dt` `TIMESTAMP`, primary key (`id`)) 

engine=MyISAM

engine=MyISAM

TIMESTAMP 周围有不需要的`".

There is unrequired '`' around TIMESTAMP.

推荐答案

可以使用@Column注解定义列类型:

You can define the column type using the @Column annotation:

@Column(columnDefinition="TIMESTAMP")  
@UpdateTimestamp
private ZonedDateTime updateDt;

这篇关于如何将 TIMESTAMP 列映射到 ZonedDateTime JPA 实体属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How can create a producer using Spring Cloud Kafka Stream 3.1(如何使用Spring Cloud Kafka Stream 3.1创建制片人)
Insert a position in a linked list Java(在链接列表中插入位置Java)
Did I write this constructor properly?(我是否正确地编写了这个构造函数?)
Head value set to null but tail value still gets displayed(Head值设置为空,但仍显示Tail值)
printing nodes from a singly-linked list(打印单链接列表中的节点)
Control namespace prefixes in web services?(控制Web服务中的命名空间前缀?)