为什么我使用H2数据库/ Spring启动应用程序获取JdbcSQLException(非十六进制字符)?

DaF*_*oot 14 java hibernate h2 spring-data-jpa spring-boot

所以简短的版本,我猜我有某种字符编码问题,或者数据库以Hibernate/Spring-jpa格式存储/返回日期由于某种原因不喜欢.

但是,如果我能弄清楚出了什么问题,我会被嘲笑!

使用Hibernate 5在实体道具中使用J8 LocalDate的东西.

正在创建数据库并插入数据(您将在下面的日志片段中看到我得到一个日期值).

日志片段:

2016-10-26 13:25:19.885 ERROR 1028 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: 
Could not read entity state from ResultSet : EntityKey[uk.co.deditech.entity.Person#2]; 
nested exception is org.hibernate.exception.GenericJDBCException: Could not read entity state from ResultSet : 
EntityKey[uk.co.deditech.entity.Person#2]] with root cause org.h2.jdbc.JdbcSQLException: Hexadecimal string contains non-hex character: "2016-03-23" [90004-192]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345) ~[h2-1.4.192.jar:1.4.192]
at org.h2.message.DbException.get(DbException.java:179) ~[h2-1.4.192.jar:1.4.192]
at org.h2.message.DbException.get(DbException.java:155) ~[h2-1.4.192.jar:1.4.192]
at org.h2.util.StringUtils.convertHexToBytes(StringUtils.java:986) ~[h2-1.4.192.jar:1.4.192]
at org.h2.value.Value.convertTo(Value.java:973) ~[h2-1.4.192.jar:1.4.192]
at org.h2.value.Value.getBytes(Value.java:422) ~[h2-1.4.192.jar:1.4.192]
at org.h2.jdbc.JdbcResultSet.getBytes(JdbcResultSet.java:1077) ~[h2-1.4.192.jar:1.4.192]
<snip>
Run Code Online (Sandbox Code Playgroud)

摇篮:

compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-freemarker")
compile group: 'com.h2database', name: 'h2', version:'1.4.192'
Run Code Online (Sandbox Code Playgroud)

实体:

@Entity
@Table(name = "person")
public @Data class Person {
   ...
   @Column(name = "last_grading_date", nullable = true)
   private LocalDate lastGradingDate;
}
Run Code Online (Sandbox Code Playgroud)

Spring启动自动数据库创建脚本片段:

schema.sql
create table PERSON
(
id int not null,
last_grading_date date
)

data.sql
insert into person (id, last_grading_date)
values (1, '2015-02-20');
Run Code Online (Sandbox Code Playgroud)

属性(问题是在我添加下面的编码属性之前和之后发生的):

spring.datasource.url=jdbc:h2:mem:AZ;DB_CLOSE_DELAY=-1;
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.datasource.sql-script-encoding=UTF-8
Run Code Online (Sandbox Code Playgroud)

编辑:经过一些挖掘,我发现"validate"是spring.jpa.hibernate.ddl-auto属性的设置.所以我试过了.

我现在在启动时收到以下错误...

Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [last_grading_date] in table [person]; found [date (Types#DATE)], but expecting [binary(255) (Types#VARBINARY)]
at org.hibernate.tool.schema.internal.SchemaValidatorImpl.validateColumnType(SchemaValidatorImpl.java:105) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
Run Code Online (Sandbox Code Playgroud)

Pat*_*ick 13

我通过在我的pom中添加这个依赖项来实现它:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-java8</artifactId>
    <version>${hibernate.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我不知道为什么它没有开箱即用,但有了这种依赖性它解决了这个问题.

我还在属性下添加了这个属性: <hibernate.version>5.0.5.Final</hibernate.version>

我的复制示例代码:

Data.sql:

insert into person (id, last_grading_date)
values (1, '2015-02-20');
Run Code Online (Sandbox Code Playgroud)

application.properties

spring.datasource.url=jdbc:h2:mem:AZ;DB_CLOSE_DELAY=-1;
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.datasource.sql-script-encoding=UTF-8
Run Code Online (Sandbox Code Playgroud)

PersonRepository

public interface PersonRepository extends JpaRepository<Person, Integer>{

}
Run Code Online (Sandbox Code Playgroud)

@Entity
@Table(name = "person")
public class Person {

    @Id
    @Column
    private int id;

    @Column(name = "last_grading_date", nullable = true)
    @Type(type = "java.time.LocalDate")
    private LocalDate lastGradingDate;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public LocalDate getLastGradingDate() {
        return lastGradingDate;
    }

    public void setLastGradingDate(LocalDate lastGradingDate) {
        this.lastGradingDate = lastGradingDate;
    }
}
Run Code Online (Sandbox Code Playgroud)

的applcation

@SpringBootApplication
public class TestApplication implements CommandLineRunner{

    @Autowired
    PersonRepository repo;

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

    @Override
    public void run(String... arg0) throws Exception {
        Person p = repo.findOne(1);
        System.out.println(p.getLastGradingDate());
    }
}
Run Code Online (Sandbox Code Playgroud)

结果: 2015-02-20


GitHub上添加了一个工作示例.该演示是建立在Spring-boot,Java 8,Hibernate 5,mavenjava.time.LocalDate.


Aso*_*oub 0

您没有指定休眠中列的类型last_grading_date。您可以使用:

@Column(name = "last_grading_date", nullable = true)
@Type(type="date")
private LocalDate lastGradingDate;
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,请更改LocalDate班级。java.sql.Date