java - 在 Hibernate 中为 LocalDateTime 创建列类型 Datetime
问题描述
使用 JPA 在 MariaDB 中创建列类型 Datetime 的正确方法是什么?我试过这个:
@Column
@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime created_at;
但我得到例外:
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
你能提出一些解决方案吗?
解决方案
您spring-boot-starter-data-jpa
具有hibernate-core
作为编译依赖项的依赖项。
如果您使用的是 spring boot 1.4.x 或更高版本,您将获得 Hibernate 5:
您只需添加以下依赖项:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
<version>5.0.0.Final</version>
</dependency>
它提供对 Java 8 LocalDateTime API 的支持。
如果您使用 Spring Boot 版本 2.xx 或更高版本spring-boot-starter-data-jpa
,则带有内置 Java 8 LocalDateTime API 的 hibernate 5.2,因此不需要额外的依赖项。
你可以简单地写:
private LocalDateTime created_at;
推荐阅读
- python - 如何将具有外键的另一个表中的多个字段添加到list_display?
- android - 如何在 Flutter 中使用 Native 应用程序 sdk
- c++ - valgrind:尝试调试在 Docker 容器内运行的 C++ 程序时,堆块 lo/hi 大小不匹配
- windows - 从android studio执行后如何停止Git for Windows终端关闭
- properties - 使用 powershell 将结果输出到 LABEL 控件中
- jsf - 动态创建的 p:datatable 中的 RowSelection 始终返回第 0 行
- c - 如何在 C 中初始化双指针?(指针数组)
- android - Flutter - 如何让 SnackBar 不向上推动 FloatingActionButton?
- c# - 基础提供程序在 Open EF6 iis 上失败
- intellij-idea - 为 IntelliJ 构建工件键盘快捷键