Search code examples
javaspring-boothibernateh2

Hibernate generating incorrect DDL for H2 in tests. Springboot 3.2.2, Hibernate 6.4.1, H2 2.2.224


I am upgrading from Spring 2.X (Java 8) to Spring 3.2.2 / Java 17 Snippet from my Entity definition:

  @Column(columnDefinition = "CHAR(1 CHAR) DEFAULT 'N'")
  private Boolean checkFlag;

  @Column(length = 64)
  private String randomKey;

In production we use Oracle, so columnDefinition has "CHAR(1 CHAR)"

In test cases, DDL generated for H2 is as follows: checkFlag CHAR(1 CHAR) DEFAULT 'N',

Problem is H2 is not honoring CHAR(1 CHAR), it expects CHAR(1)

I tried setting various Mode in H2 URL: like Oracle, Legacy. Nothing helps. This used to work in Springboot 2.x


Solution

  • If custom column definition is specified in the annotation, JPA uses it as is, correctness of this definition is under your responsibility.

    The simplest solution here is to use CHAR instead of non-standard CHAR(1 CHAR), because 1 is the default anyway.

    H2 is able to parse Oracle-specific length too, but only in the Oracle compatibility mode, in other modes only standard length specification is allowed. To pass custom settings to H2 you need to disable automatic configuration of JDBC URL for unit tests with @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE or in any other way, otherwise your custom JDBC URL with these settings will be ignored.