如何将TIMESTAMP列映射到ZonedDateTime JPA实体属性? [英] How to map a TIMESTAMP column to a ZonedDateTime JPA entity property?

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

问题描述

我使用的是Spring数据 jpa mariadb 最新版本,以及 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_dtschedule_dtupdate_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实体属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆