引起:org.h2.jdbc.JdbcSQLDataException: Hexadecimal string contains non-hex character

ann*_*erg 6 java spring jpa jdbc

我正在尝试使用内存数据库编写测试。我写了一个 sql 来清理数据并将数据存储到 DB。但我有一个例外:

Caused by: org.h2.jdbc.JdbcSQLDataException: Hexadecimal string contains non-hex character: "e7485042-b46b-11e9-986a-b74e614de0b0"; SQL statement:
insert into users (user_id, name, created_on, modified_on) values ('e7485042-b46b-11e9-986a-b74e614de0b0', 'Ann', null, null) -- ('e7485042-b46b-11e9-986a-b74e614de0b0', 'Ann', NULL, NULL) [90004-199]
Run Code Online (Sandbox Code Playgroud)

我的sql:

insert into users (user_id, name, created_on, modified_on) values ('e7485042-b46b-11e9-986a-b74e614de0b0', 'Ann', null, null);

insert into product(product_id, name, created_on, modified_on) VALUES ('f3a775de-b46b-11e9-95e4-af440b6044e6', 'product1', '2019-08-01 17:51:51.000000', '2019-08-01 17:51:51.000000');

insert into products_users(user_id, product_id) VALUES ('e7485042-b46b-11e9-986a-b74e614de0b0', 'f3a775de-b46b-11e9-95e4-af440b6044e6');
Run Code Online (Sandbox Code Playgroud)

我的 application.properties:

spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
Run Code Online (Sandbox Code Playgroud)

kma*_*bet 10

使用spring.datasource.url=jdbc:h2:mem:testdb;MODE=MYSQL为我修复了它。

或者向 UUID 字段添加注释 @Type 应该可以解决问题:

@Id
@Type(type="uuid-char")
private UUID user_id;
Run Code Online (Sandbox Code Playgroud)


sku*_*ski 9

这个问题的实际原因是你的对象和用于创建 h2 数据库模式create table的 hibernate ( )生成的语句之间的映射。ddl-auto:create

如果您使用以下命令启用这些语句的输出ddl

spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.type=TRACE
Run Code Online (Sandbox Code Playgroud)

您很可能会看到您的UUID类已映射到数据库中的二进制列。

Hibernate: 
    create table <your_table> (
        id bigint generated by default as identity,
        ...,
        <your_object> binary(255),
        ...
        primary key (id)
    )
Run Code Online (Sandbox Code Playgroud)

这意味着您的 uuid 字符串被映射到二进制列,因此包含非法字符。您需要一个 varchar(<uuid-length>)列来存储 uuid。有几种解决策略,其中之一是定义类型,请参阅此StackOverflow 答案。您可以阅读MySQLbinary官方参考网站上的专栏。


ann*_*erg -5

spring.jpa.hibernate.ddl-auto=none我通过添加到我的application.properties文件解决了这个问题