1

Let's consider a simple Postgres database with only one table and one column, that is of type DATE, i.e.:

CREATE DATABASE test;
CREATE TABLE test_table
(
    date_test DATE NOT NULL DEFAULT CURRENT_DATE
);

From the Hibernate documentation regarding basic types and PostgresSQL documentation regarding using Java 8 Date and Time classes, I can clearly see that I should be able to map that table like this:

@Entity
@Table(name = "test_table")
public class TestTable
{
    @Column(name = "date_test")
    private LocalDate dateTest;
}

quite easily, without a need to write @Temporal or any stuff alike. From the Hibernate documentation I can read:

Because the mapping between Java 8 Date/Time classes and the SQL types is implicit, there is not need to specify the @Temporal annotation. Setting it on the java.time classes throws the following exception: org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar property

However, I'm getting an error

Schema-validation: wrong column type encountered in column [date_test] in table [test_table]; found [date (Types#DATE)], but expecting [bytea (Types#VARBINARY)]

In my pom.xml file I've got:

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.4-1206-jdbc42</version>
</dependency>

and since I'm also using Spring Boot, in application.properties I've got

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect

Am I wrong about documentation? Should I write a Converter or is there any other way?

EDIT
Currently my pom.xml file looks like this:

<?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>

and now I'm getting:

java.lang.NoSuchMethodError:org.hibernate.engine.spi.Session‌​FactoryImplementor.g‌​etProperties()Ljava/‌​util/Properties;
4

2 回答 2

1

感谢 Ruslan K. 的建议和这个答案https://stackoverflow.com/a/44455146/4671908我能够pom.xml正确修改我的。不幸的是spring-boot-starter-data-jpa 1.5.6.RELEASE,单独不支持最新的 Hibernate 修改,所以我排除了hibernate-corehibernate-entitymanagerfrom pom.xml,现在看起来像这样

        <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>
于 2017-08-11T11:03:31.873 回答
1

您应该使用更新版本的 Hibernate。

我不能说哪个版本开始支持 Java 8 Dates,但在我的项目中我使用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 。

于 2017-08-11T10:03:12.560 回答