简体   繁体   English

Spring 中的问题 使用 H2 启动 Hibernate 以测试模拟 PostgreSQL

[英]Issues in Spring Boot with Hibernate using H2 for testing simulating PostgreSQL

I am facing issue running a spring boot test which is simulating a h2 database for postgresql .我在运行 spring 启动测试时遇到问题,该测试正在模拟postgresqlh2数据库。

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)

Why Column "currencyen0_.isbasecurrency" is missing?为什么列"currencyen0_.isbasecurrency"丢失了? My entity currency does have this column defined.我的实体currency确实定义了此列。

My Test properties looks like this.我的测试属性看起来像这样。

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;

If you use H2 DB then don't you have to use H2 Dialect?如果您使用 H2 DB 那么您是否必须使用 H2 方言?

enter link description here 在此处输入链接描述

Unfortunatelly I can not reproduce your bug.不幸的是我无法重现你的错误。 Please try to add your Entity to the post.请尝试将您的实体添加到帖子中。

Another way is to use TestContainers to emulate postgres.另一种方法是使用 TestContainers 来模拟 postgres。 I want to include this recipe for records.我想把这个食谱包括在内作为记录。

NOTE: Docker is needed to run TestContainers.注意:运行 TestContainers 需要 Docker。

application.yml will looks like: application.yml看起来像:

spring:
  flyway:
    locations: classpath:db/migration
  datasource:
    driver-class-name: org.postgresql.Driver
    username: postgres
    password:
    url: jdbc:postgresql://localhost:5432/test

You can change flyway to another migration tool that you like.您可以将 flyway 更改为您喜欢的其他迁移工具。

application-test.yml will look like: application-test.yml看起来像:

spring:
  datasource:
    driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
    url: jdbc:tc:postgresql:11.1///test
  jpa:
    database-platform: org.hibernate.dialect.PostgreSQL9Dialect
    hibernate:
      ddl-auto: validate

pom.xml should contain following dependencies: pom.xml应该包含以下依赖项:

    <properties>
        <org.testcontainers.version>1.12.5</org.testcontainers.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>testcontainers</artifactId>
            <version>${org.testcontainers.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>postgresql</artifactId>
            <version>${org.testcontainers.version}</version>
            <scope>test</scope>
        </dependency>
        ...
    </dependencies>

Test should look like:测试应如下所示:

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class TestcontainerApplicationTests {
    @Autowired
    UserRepository userRepository;

    @Test
    void contextLoads() {
        User user = new User();
        user.setName("test");
        userRepository.save(user);
    }
}

This code snipplet will add TestContainer.此代码片段将添加 TestContainer。 It will run PostgreSQL database in docker. Hope it will be helpful for someone.它将在docker中运行PostgreSQL数据库。希望对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM