spring JPA不向H2选择查询添加双引号

Luk*_*ela 2 hibernate jpa h2 spring-data spring-data-jpa

我的配置:

spring.datasource.url=jdbc:h2:mem:db;SCHEMA=public;DB_CLOSE_DELAY=-1;
Run Code Online (Sandbox Code Playgroud)

JPA 生成以下查询:

select itement0_.id as id1_0_, itement0_.brand as brand2_0_, itement0_._item_id as item3_0_, itement0_.product_group as product_4_0_ from public.item itement0_ where itement0_.item_id=? [42102-200]
Run Code Online (Sandbox Code Playgroud)

爆炸了

org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "ITEM" not found; SQL statement
Run Code Online (Sandbox Code Playgroud)

据我所知,问题是 h2 期望双引号中的公共模式,因此以下查询在 h2 控制台中工作正常:

    select itement0_.id as id1_0_, itement0_.brand as brand2_0_, itement0_._item_id as item3_0_, itement0_.product_group as product_4_0_ from "public".item itement0_ where itement0_.item_id=? [42102-200]
Run Code Online (Sandbox Code Playgroud)

我的实体:

@Getter
@Entity(name = "item")
@Table(name="item", schema = "public")
@AllArgsConstructor
@NoArgsConstructor(force = true)

public class ItemEntity {

@Id
@GeneratedValue(generator="UUID")
private final UUID id;

@NotNull
@Column(unique = true)
private final String itemId;

private final String productGroup;

private final String brand;

}
Run Code Online (Sandbox Code Playgroud)

Flyway创建脚本:

    CREATE TABLE IF NOT EXISTS item
(
    id            uuid,
    item_id       VARCHAR,
    product_group VARCHAR,
    brand         VARCHAR,
    constraint pk_item PRIMARY KEY (id)
);
Run Code Online (Sandbox Code Playgroud)

h2 中的结果结构: h2 中的结果结构

有人知道一些解决方法吗?

Sim*_*lli 6

您可以将 hibernate.globally_quoted_identifiers 参数添加到您的 application.properties 中:

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
Run Code Online (Sandbox Code Playgroud)