新的 H2 版本打破了 liquibase

New H2 version breaks liquibase

新版本的H2好像破解了liquibase和JPA

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.h2test</groupId>
    <artifactId>h2test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>h2test</name>
    <description>h2test</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.6.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
            <version>4.7.1</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>2.1.210</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

application.properties

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

类:

package com.h2test.h2test.domain;

import lombok.Data;

import javax.persistence.*;

@Entity
@Data
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String firstName;

    @Column
    private String lastName;
    
}

package com.h2test.h2test.repository;

import com.h2test.h2test.domain.Person;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface PersonRepository extends JpaRepository<Person, Long> {
}

@RestController
public class SomethingController {

    @Autowired
    private PersonRepository personRepository;

    @GetMapping
    public String test() {

        Person person = new Person();
        person.setFirstName("first name");
        person.setLastName("last name");

        personRepository.save(person);
        return "hello";
    }
}

Liquibase 文件:

databaseChangeLog:
  - changeSet:
      id:  createTable-example
      author:  liquibase-docs
      changes:
        -  createTable:
             tableName:  person
             columns:
               -  column:
                    name:  id
                    type:  int
                    autoIncrement:  true
                    constraints:
                      primaryKey:  true
                      nullable:  false
               -  column:
                    name:  first_name
                    type:  varchar(50)
               -  column:
                    name:  last_name
                    type:  varchar(50)
                    constraints:
                      nullable:  false

当触发创建并保存 Person 实体的 REST ENDPOINT localhost:8080 时,出现以下错误:

org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException:列“ID”不允许为 NULL; SQL声明: 插入人 (id, first_name, last_name) 值 (null, ?, ?) [23502-202] 在 org.h2.message.DbException.getJdbcSQLException(DbException.java:508) ~[h2-2.0.202.jar:2.0.202] 在 org.h2.message.DbException.getJdbcSQLException(DbException.java:477) ~[h2-2.0.202.jar:2.0.202]

这似乎只发生在较新版本的 H2 中。感谢任何帮助

spring-boot-starter-data-jpa 的 2.6.3 版在其依赖项中有 org.hibernate:hibernate-core:5.6.4.Final。此版本不支持 H2 2.x.y。您需要使用 5.6.5:

<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-core</artifactId>
   <version>5.6.5.Final</version>
</dependency>

您的类路径也有问题。您的依赖项中有 H2 2.1.210,但您的问题中的异常有错误代码 23502-202,这意味着它是由 H2 2.0.202 生成的,您不应该使用这个过时的版本,Hibernate ORM 可能有一些问题。