【问题标题】:Spring JPA with DB2 error SqlSyntaxErrorException带有 DB2 错误 SqlSyntaxErrorException 的 Spring JPA
【发布时间】:2021-10-12 02:54:35
【问题描述】:

我正在尝试创建一个 Spring Boot 应用程序来从 DB2 视图中读取数据

我的 application.properties 的数据源为:

spring.datasource.url=jdbc:db2://servername:port/dbname
spring.datasource.username=me
spring.datasource.password=mypass
spring.datasource.driver-class-name=com.ibm.db2.jcc.DB2Driver
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.DB2390Dialect

当我运行我的应用程序时,它会出现以下错误:

com.ibm.db2.jcc.am.SqlSyntaxErrorException:DB2 SQL 错误:SQLCODE=-551, SQLSTATE=42501, SQLERRMC=me;CREATE TABLE;DSNDB04, DRIVER=4.29.24

根据我的阅读,这个错误意味着用户“我”没有运行“CREATE TABLE”操作的权限。

如果我将 application.properties 中的上述数据源设置替换为使用 H2 数据库,则一切正常。所以,我知道我的 java 类等都工作正常,但我不确定如何解决这个问题并从 DB2 视图读取数据。

顺便说一句,因为我在这里谈论数据库视图,所以我的实体用@Immutable 注释:

@Entity
@Immutable
@Table(name = "CAR_VIEW") 
public class CarEntity implements Serializable {
   ...
}

如上所述,在 H2 中运行它会创建 CAR_VIEW,我可以在其中使用 CommandLineRunner 预填充一些数据,然后我还可以从 Postman 查询该数据。

但是,我的 DB2 数据库已经有了这个视图和它提供的数据(通过连接几个表来获取视图中所需的数据)。所以对于 DB2 数据库,我不希望 JPA/Hibernate 创建新的CAR_VIEW(因为我使用的是ddl-auto=update,所以不应该这样),所以它应该看到视图已经存在并且我不应该得到这个错误。

【问题讨论】:

    标签: spring-boot spring-data-jpa db2


    【解决方案1】:

    事实证明,我需要为我的实体类提供架构,所以一旦我将架构添加到表注释中,问题就已经解决了。

    与上述相比,我的实体类现在具有如下注释:

    @Entity
    @Immutable
    @Table(name = "CAR_VIEW", schema = "MY_SCHEMA") 
    public class CarEntity implements Serializable {
       ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-20
      • 2018-10-12
      • 2018-06-15
      • 2015-02-01
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      • 2014-09-30
      相关资源
      最近更新 更多