spring-data-jpa 当通过Spring Data JPA在Entity类中更改时,Liquibase不更新列数据类型

jaxagkaj  于 2022-11-10  发布在  Spring
关注(0)|答案(2)|浏览(194)

在 Boot ,SpringDataJPA项目中,我有一个名为Country的实体类,它包含几个列。

$ mvn process-test-resources

$ mvn process-resources

它使用liquibase和liquibase-hibernate 5插件创建了表格

国家/地区.java

package com.liquibasedemo.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import javax.persistence.*;

@Entity
@Table(name = "country")
@Data
public class Country
{
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "code")
    private String code;

    @Column(name = "iso_code")
    private String isoCode;

    @Column(name = "test_code")
    private String testCode;

    @ManyToOne
    @JoinColumn(name = "region_id")
    @JsonIgnore
    private Region region;

}

我在Country类中将testCode列的数据类型从Long更新为String,并使用上面的命令生成了变更集。

pom.xml文件

<?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 http://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.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com</groupId>
    <artifactId>liquibasedemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>liquibasedemo</name>
    <description>Liquibase Demo project with Spring Boot and Spring Data</description>

    <properties>
        <spring-boot.version>2.1.5.RELEASE</spring-boot.version>
        <hibernate.version>5.4.3.Final</hibernate.version>
        <liquibase-maven-plugin.version>3.5.5</liquibase-maven-plugin.version>
        <liquibase-hibernate5.version>3.6</liquibase-hibernate5.version>
        <validation-api.version>2.0.1.Final</validation-api.version>
        <javassist.version>3.24.0-GA</javassist.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-envers</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </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>
            </plugin>

            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>${liquibase-maven-plugin.version}</version>
                <configuration>
                    <propertyFile>src/main/resources/liquibase.properties</propertyFile>
                    <changeLogFile>src/main/resources/db/db.changelog-master.xml</changeLogFile>
                    <diffChangeLogFile>src/main/resources/db/changelog/${maven.build.timestamp}_changelog.xml</diffChangeLogFile>
                    <logging>info</logging>
                </configuration>

                <executions>
                    <execution>
                        <id>update-profile</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>update</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>diff-profile</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>diff</goal>
                        </goals>
                    </execution>
                </executions>

                <dependencies>
                    <dependency>
                        <groupId>org.liquibase.ext</groupId>
                        <artifactId>liquibase-hibernate5</artifactId>
                        <version>${liquibase-hibernate5.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-data-jpa</artifactId>
                        <version>${spring-boot.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>javax.validation</groupId>
                        <artifactId>validation-api</artifactId>
                        <version>${validation-api.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.javassist</groupId>
                        <artifactId>javassist</artifactId>
                        <version>${javassist.version}</version>
                    </dependency>

                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>
weylhg0b

weylhg0b1#

此使用liquibase-hibernate5生成的变更集不包括字段类型更改。如果您可以查看changeset文件,您将不会发现生成的与字段类型更改相关的changesetliquibase-hibernate5插件似乎不支持此操作。
1.请查看此class中的注解
1.这个插件的开发者正在讨论它的链接
之所以忽略这些,是因为Hibernate表示数据类型的方式通常不同于数据库表示数据类型的方式,即使它们实际上是相同的。我正在寻找用Liquibase 4改进这一点的方法。
1.现在的解决方案是手动添加diff

<changeSet author="liquibase-docs" id="modifyDataType-example">
    <modifyDataType catalogName="cat"
            columnName="id"
            newDataType="int"
            schemaName="public"
            tableName="person"/>
</changeSet>
j7dteeu8

j7dteeu82#

您可以只将ChangeSet添加到您的xml文件中(如果您使用的是xml而不是yaml或其他文件类型)。

<changeSet id="change type" author="me">
    <modifyDataType tableName="Country" columnName="name" 
           newDataType="VARCHAR(255)"/>
</changeSet>

有关添加、重命名和删除列的详细信息,请查看此链接:我希望这能帮上忙

相关问题