java 使用H2测试模拟PostgreSQL的Hibernate的Sping Boot 中的问题

m1m5dgzv  于 2023-01-01  发布在  Java
关注(0)|答案(1)|浏览(143)

我在运行模拟postgresqlh2数据库的Spring Boot 测试时遇到问题。

org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [select currencyen0_.currency as currency1_0_0_, currencyen0_.isBaseCurrency as isbasecu2_0_0_ from currency currencyen0_ where currencyen0_.currency=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement

    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:281)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:528)
    -----
    -----

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Column "currencyen0_.isbasecurrency" not found; SQL statement:
select currencyen0_.currency as currency1_0_0_, currencyen0_.isBaseCurrency as isbasecu2_0_0_ from currency currencyen0_ where currencyen0_.currency=? [42122-200]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:453)
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:429)
    at org.h2.message.DbException.get(DbException.java:205)
    at org.h2.message.DbException.get(DbException.java:181)
    at org.h2.expression.ExpressionColumn.getColumnException(ExpressionColumn.java:163)
    at org.h2.expression.ExpressionColumn.optimize(ExpressionColumn.java:145)
    at org.h2.expression.Alias.optimize(Alias.java:52)
    at org.h2.command.dml.Select.prepare(Select.java:1206)
    at org.h2.command.Parser.prepareCommand(Parser.java:744)
    at org.h2.engine.Session.prepareLocal(Session.java:657)
    at org.h2.engine.Session.prepareCommand(Session.java:595)
    at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1235)
    at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:76)
    at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:352)
    at com.zaxxer.hikari.pool.ProxyConnection.prepareStatement(ProxyConnection.java:337)
    at com.zaxxer.hikari.pool.HikariProxyConnection.prepareStatement(HikariProxyConnection.java)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:149)

为什么缺少列"currencyen0_.isbasecurrency"?我的实体currency定义了此列。
我的测试属性如下所示。

spring:
  output.ansi.enabled: ALWAYS
  jpa:
    # Tried 1 : workaround org.hibernate.dialect.H2Dialect
    # Tried 2 : org.hibernate.dialect.PostgreSQL95Dialect
    database-platform: org.hibernate.dialect.PostgreSQLDialect
    generate-ddl: true
    hibernate.ddl-auto: create
    show-sql: true
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
        id.new_generator_mappings: false
        format_sql: true
  datasource:
    driverClassName: org.h2.Driver
    # Tried 3 :these additional settings DATABASE_TO_LOWER=TRUE;MODE=PostgreSQL;
    url: jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1;
k3fezbri

k3fezbri1#

如果你使用H2 DB,那么你不需要使用H2方言吗?
See this

相关问题