简体   繁体   English

在Hibernate中创建列类型Datetime

[英]Create column type Datetime in Hibernate

What is the proper way to create column type Datetime in MariaDB using Hibernate? 使用Hibernate在MariaDB中创建列类型Datetime的正确方法是什么? I tried this: 我尝试了这个:

@Column
@Type(type = "date")
@Temporal(TemporalType.DATE)
private Date created_at;

But I can't find a proper type in TemporalType for Datetime. 但是我在TemporalType中找不到适用于Datetime的类型。

您需要使用TemporalType.TIMESTAMP作为DateTime。

This is what you need to do. 这是您需要做的。

@Column
@Temporal(TemporalType.TIMESTAMP)
private Date created_at;

You don't need to write @Type(type = "date") . 您无需编写@Type(type = "date") And change your TemporalType to TimeStamp . 并将您的TemporalType更改为TimeStamp So finally your code will be like this; 所以最终您的代码将像这样;

@Column
@Temporal(TemporalType.TIMESTAMP)
private Date created_at;

PS: You don't need to write @Column tag. PS:您不需要编写@Column标记。

Use the columnDefinition attribute of the @Column annotation: 使用@Column批注的columnDefinition属性:

@Column(name = "startTime", columnDefinition="DATETIME")
@Temporal(TemporalType.TIMESTAMP)
private Date startTime;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM