如何将 TIMESTAMP 列映射到 ZonedDateTime JPA 实体属性?
我正在使用 Spring data 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_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;
相关文章