Home > Software engineering >  Issues in Spring Boot with Hibernate using H2 for testing simulating PostgreSQL
Issues in Spring Boot with Hibernate using H2 for testing simulating PostgreSQL

Time:01-05

I am facing issue running a spring boot test which is simulating a h2 database for postgresql.

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 ? My entity currency does have this column defined.

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;

CodePudding user response:

If you use H2 DB then don't you have to use H2 Dialect?

See this

CodePudding user response:

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. I want to include this recipe for records.

NOTE: Docker is needed to run TestContainers.

application.yml will looks like:

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.

application-test.yml will look like:

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:

    <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. It will run PostgreSQL database in docker. Hope it will be helpful for someone.

  • Related