【问题标题】:Spring JPA H2 database get org.h2.jdbc.JdbcSQLSyntaxErrorException Schema "MYSCHEMA" not foundSpring JPA H2 数据库获取 org.h2.jdbc.JdbcSQLSyntaxErrorException 架构“MYSCHEMA”未找到
【发布时间】:2021-12-01 10:42:02
【问题描述】:

我的 Spring boot JPA 实体定义为:

@Entity
@Immutable
@Table(name = "CAR", schema="MYSCHEMA") //DB2 requires schema so this works on DB2 but not on H2
//@Table(name = "CAR") // this works on H2 but not on DB2 as it requires schema
public class CarEntity implements Serializable {
...

上述方法在 DB2 中可以正常工作,但在 H2 中却不行。

在 application.properties 中,我有以下设置:

spring.datasource.url=jdbc:h2:mem:MYDB"
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.validationQuery=SELECT 1

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect

当我执行 maven>Install 时,出现错误: org.hibernate.tool.schema.spi.CommandAcceptanceException:执行 DDL 时出错“ 创建表 MYSCHEMA.CAR (... ...

原因:org.h2.jdbc.JdbcSQLSyntaxErrorException:找不到架构“MYSCHEMA”; SQL 语句: 公共类 CarEntity 实现可序列化 { ...

我希望在 H2 上创建模式,因为我在 @Table 注释中提供了它,但这并没有发生。我错过了什么?

【问题讨论】:

    标签: spring-boot hibernate spring-data-jpa h2 database-schema


    【解决方案1】:

    想出了如何解决这个问题。 由于 DB2 需要模式,我改为如下:

    标记实体
    @Entity
    @Immutable
    @Table(name = "CAR") 
    public class CarEntity implements Serializable {
    

    但对于 DB2 url,将 :currentSchema=MY_SCHEMA; 添加到 spring 数据源 url 的末尾,如:

    spring.datasource.url=jdbc:db2://db2_server:port//MY_DB:currentSchema=MY_SCHEMA;
    

    需要注意的是 MY_DB 和 currentSchema 之间的“:”分隔符和“;”最后。

    对于 H2,我不关心架构,因为它是内存数据库,我只是用来测试结果端点,我的实体的 @Table 注释和数据源 url 中都没有任何架构指示:

    spring.datasource.url=jdbc:h2:mem:MY_DB

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-16
      • 1970-01-01
      • 2019-12-17
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      相关资源
      最近更新 更多