Java 8 中使用 Hibernate 5+ 的 PostgreSQL 9.6 DATE 列映射
PostgreSQL 9.6 DATE column mapping using Hibernate 5+ in Java 8
让我们考虑一个简单的 Postgres 数据库,它只有一个 table 和一列,类型为 DATE,即:
CREATE DATABASE test;
CREATE TABLE test_table
(
date_test DATE NOT NULL DEFAULT CURRENT_DATE
);
从 Hibernate documentation regarding basic types and PostgresSQL documentation 关于使用 Java 8 日期和时间 类,我可以清楚地看到我应该能够像这样映射 table:
@Entity
@Table(name = "test_table")
public class TestTable
{
@Column(name = "date_test")
private LocalDate dateTest;
}
很容易,不需要写 @Temporal
或任何类似的东西。从 Hibernate 文档中我可以读到:
因为Java8Date/Time类和SQL类型之间的映射是隐式的,所以不需要指定@Temporal注解。在 java.time 类 上设置它会引发以下异常:
org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar property
但是,我遇到了一个错误
Schema-validation: wrong column type encountered in column [date_test] in table [test_table]; found [date (Types#DATE)], but expecting [bytea (Types#VARBINARY)]
在我的 pom.xml
文件中我有:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1206-jdbc42</version>
</dependency>
并且因为我也在使用 Spring 启动,所以在 application.properties
我有
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect
我对文档的理解有误吗?我应该写一个转换器还是有其他方法?
编辑
目前我的 pom.xml
文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>Invoices-desktop</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<resources>
<!-- copy fxml and css resources -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.fxml</include>
<include>**/*.css</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/CurrencyDemo/java</directory>
<includes>
<include>**/*.fxml</include>
<include>**/*.css</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.Main</start-class>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.5.6.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>org.hibernate</artifactId>
<groupId>hibernate-core</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1206-jdbc42</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-CurrencyDemo</artifactId>
<scope>CurrencyDemo</scope>
</dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>59.1</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.7</version>
</dependency>
</dependencies>
</project>
现在我得到:
java.lang.NoSuchMethodError:org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava/util/Properties;
您应该使用更新版本的 Hibernate。
我不能说哪个版本开始支持Java8个日期,但在我的项目中我使用hibernate-core:5.2.6.Final.
我在使用 spring-boot-starter-data-jpa:1.5.6.RELEASE 时遇到了类似的问题,这取决于 hibernate-core:5.0.12.Final.
注意:如果你使用了spring-boot-starter-data-jpa,你应该排除hibernate-core 来自依赖项。
感谢 Ruslan K. 的建议和这个答案 我能够正确地修改我的 pom.xml
。不幸的是 spring-boot-starter-data-jpa 1.5.6.RELEASE
单独不支持最新的 Hibernate 修改,所以我从 pom.xml
中排除了 hibernate-core
和 hibernate-entitymanager
,现在看起来像这样
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.5.6.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>org.hibernate</artifactId>
<groupId>hibernate-core</groupId>
</exclusion>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
让我们考虑一个简单的 Postgres 数据库,它只有一个 table 和一列,类型为 DATE,即:
CREATE DATABASE test;
CREATE TABLE test_table
(
date_test DATE NOT NULL DEFAULT CURRENT_DATE
);
从 Hibernate documentation regarding basic types and PostgresSQL documentation 关于使用 Java 8 日期和时间 类,我可以清楚地看到我应该能够像这样映射 table:
@Entity
@Table(name = "test_table")
public class TestTable
{
@Column(name = "date_test")
private LocalDate dateTest;
}
很容易,不需要写 @Temporal
或任何类似的东西。从 Hibernate 文档中我可以读到:
因为Java8Date/Time类和SQL类型之间的映射是隐式的,所以不需要指定@Temporal注解。在 java.time 类 上设置它会引发以下异常:
org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar property
但是,我遇到了一个错误
Schema-validation: wrong column type encountered in column [date_test] in table [test_table]; found [date (Types#DATE)], but expecting [bytea (Types#VARBINARY)]
在我的 pom.xml
文件中我有:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1206-jdbc42</version>
</dependency>
并且因为我也在使用 Spring 启动,所以在 application.properties
我有
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect
我对文档的理解有误吗?我应该写一个转换器还是有其他方法?
编辑
目前我的 pom.xml
文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>Invoices-desktop</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<resources>
<!-- copy fxml and css resources -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.fxml</include>
<include>**/*.css</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/CurrencyDemo/java</directory>
<includes>
<include>**/*.fxml</include>
<include>**/*.css</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.Main</start-class>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.5.6.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>org.hibernate</artifactId>
<groupId>hibernate-core</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1206-jdbc42</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-CurrencyDemo</artifactId>
<scope>CurrencyDemo</scope>
</dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>59.1</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.7</version>
</dependency>
</dependencies>
</project>
现在我得到:
java.lang.NoSuchMethodError:org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava/util/Properties;
您应该使用更新版本的 Hibernate。
我不能说哪个版本开始支持Java8个日期,但在我的项目中我使用hibernate-core:5.2.6.Final.
我在使用 spring-boot-starter-data-jpa:1.5.6.RELEASE 时遇到了类似的问题,这取决于 hibernate-core:5.0.12.Final.
注意:如果你使用了spring-boot-starter-data-jpa,你应该排除hibernate-core 来自依赖项。
感谢 Ruslan K. 的建议和这个答案 我能够正确地修改我的 pom.xml
。不幸的是 spring-boot-starter-data-jpa 1.5.6.RELEASE
单独不支持最新的 Hibernate 修改,所以我从 pom.xml
中排除了 hibernate-core
和 hibernate-entitymanager
,现在看起来像这样
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.5.6.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>org.hibernate</artifactId>
<groupId>hibernate-core</groupId>
</exclusion>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>