简体   繁体   English

在Hibernate中为LocalDateTime创建列类型Datetime

[英]Create column type Datetime in Hibernate for LocalDateTime

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

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

But I get exception: 但我得到例外:

Caused by: org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar property: org.plugin.entity.Transactions.created_at

Can you propose some solution? 你能提出一些解决方案吗?

You have the spring-boot-starter-data-jpa dependency which has hibernate-core as a compile dependency. 您具有spring-boot-starter-data-jpa依赖关系,该依赖关系具有hibernate-core作为编译依赖关系。

If you are using spring boot 1.4.x or higher you get Hibernate 5: 如果您使用的是Spring Boot 1.4.x或更高版本,则会获得Hibernate 5:

https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa/1.4.0.RELEASE https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa/1.4.0.RELEASE

The you just have to add the following dependency: 您只需添加以下依赖项:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-java8</artifactId>
    <version>5.0.0.Final</version>
</dependency>

which provides support for Java 8 LocalDateTime API. 提供对Java 8 LocalDateTime API的支持。

If you are using Spring boot version 2.xx or higher spring-boot-starter-data-jpa ships with hibernate 5.2 which has Java 8 LocalDateTime API build in so no additional dependencies are necessary. 如果您使用的是Spring引导版本2.xx或更高版本,则spring-boot-starter-data-jpa随hibernate 5.2一起提供,其内置了Java 8 LocalDateTime API,因此不需要其他依赖项。

You can simply write: 您可以简单地写:

private LocalDateTime created_at;

Update version of Hibernate to 5.x or latest, there is 将Hibernate的版本更新为5.x或最新版本,

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-java8</artifactId>
    <version>5.0.0.Final</version>
</dependency>

You don't have to do anything else. 您无需执行其他任何操作。 Just add the dependency. 只需添加依赖项即可。 Of course other Java8 types should also work. 当然,其他Java8类型也应该起作用。

private LocalDateTime created_at;

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

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