本文介绍了使用Liquibase和JPA实体生成变更日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想将liquibase引入我的项目,首先我想根据我的休眠实体生成一个changelog文件.

I would like to introduce liquibase to my project and first of all I would like to generate a changelog file according my hibernate entities.

到目前为止我所拥有的:

What I have so far:

application.properties

# HIBERNATE
# Write out schema into sql script
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=target/schema-h2.sql
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect

# DATABASE
spring.datasource.platform=h2
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true

# LIQUIBASE
spring.liquibase.enabled=true
spring.liquibase.url=jdbc:h2:mem:testdb
spring.liquibase.user=sa
spring.liquibase.password=
spring.liquibase.changeLog=classpath:/db/h2/liquibase-changelog-master.xml

pom.xml

<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-maven-plugin</artifactId>
    <version>${liquibase.version}</version>
</dependency>

...

<plugins>
    <plugin>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-maven-plugin</artifactId>
        <version>${liquibase.version}</version>
        <configuration>
            <changeLogFile>s/m/r/db/h2/liquibase-changelog-master.xml</changeLogFile>
            <diffChangeLogFile> s/m/r/db/h2/changelog/${timestamp}_changelog.xml</diffChangeLogFile>
            <outputChangeLogFile> s/m/r/db/h2/changelog/myaddon.db.changelog.xml</outputChangeLogFile>
            <driver>org.h2.Driver</driver>
            <url>jdbc:h2:mem:testdb</url>
            <username>sa</username>
            <password></password>
            <referenceUrl>hibernate:spring:ch.hemisoft.immo.domain?hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy</referenceUrl>
            <verbose>true</verbose>
            <logging>trace</logging>
        </configuration>
        <dependencies>
            <dependency>
               <groupId>org.liquibase.ext</groupId>
               <artifactId>liquibase-hibernate5</artifactId>
               <version>${liquibase-hibernate5.version}</version>
            </dependency>
            <dependency>
               <groupId>org.liquibase</groupId>
               <artifactId>liquibase-core</artifactId>
               <version>${liquibase.version}</version>
            </dependency>
            <dependency>
               <groupId>org.springframework</groupId>
               <artifactId>spring-beans</artifactId>
               <version>${spring.version}</version>
            </dependency>
            <dependency>
               <groupId>org.springframework</groupId>
               <artifactId>spring-core</artifactId>
               <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
                <version>2.1.4.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>javax.validation</groupId>
                <artifactId>validation-api</artifactId>
                <version>${validation-api.version}</version>
            </dependency>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <version>${h2.version}</version>
            </dependency>
            <dependency>
               <groupId>org.javassist</groupId>
               <artifactId>javassist</artifactId>
               <version>${javassist.version}</version>
            </dependency>
        </dependencies>
    </plugin>
</plugins>

当尝试使用mvn liquibase:generateChangeLog生成变更日志文件时,我得到类似的东西:

When trying to generate the changelog file using mvn liquibase:generateChangeLog then I get something like:

// A big list of artifacts ...

[INFO] Starte Liquibase am So, 28 Apr 2019 19:09:24 MESZ (Version 3.6.3, kompiliert am 2019-01-29 11:34:48)
[INFO] Settings
----------------------------
[INFO]     driver: org.h2.Driver
[INFO]     url: jdbc:h2:mem:testdb
[INFO]     username: sa
[INFO]     password: *****
[INFO]     use empty password: false
[INFO]     properties file: null
[INFO]     properties file will override? false
[INFO]     prompt on non-local database? true
[INFO]     clear checksums? false
[INFO]     defaultSchemaName: null
[INFO]     diffTypes: null
[INFO]     dataDir: null
[INFO] ------------------------------------------------------------------------
[INFO] Executing on Database: jdbc:h2:mem:testdb
[INFO] Loading artifacts into URLClassLoader

// A big list of artifacts ...

[INFO] Generating Change Log from database SA @ jdbc:h2:mem:testdb (Default Schema: PUBLIC)
[INFO] SELECT SEQUENCE_NAME FROM INFORMATION_SCHEMA.SEQUENCES WHERE SEQUENCE_SCHEMA = 'PUBLIC' AND IS_GENERATED=FALSE
[INFO] src\main\resources\db\h2\changelog\myaddon.db.changelog.xml exists, appending
[INFO] No changes found, nothing to do
[INFO] Output written to Change Log file, src/main/resources/db/h2/changelog/myaddon.db.changelog.xml
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.161 s
[INFO] Finished at: 2019-04-28T19:09:26+02:00
[INFO] ------------------------------------------------------------------------

如您所见,它显示为No changes found, nothing to do,但我希望我的第一个变更日志包含初始表的创建.

As you see, it says No changes found, nothing to do, but I would expect having my first changelog with initial table creations.

我也看到了,似乎它试图从空数据库而不是从休眠类创建一个changelog文件:

What I also see, it seems it tries to create a changelog file from the empty database instead from hibernate classes:

Generating Change Log from database SA @ jdbc:h2:mem:testdb (Default Schema: PUBLIC)

输出文件myaddon.db.changelog.xml如下所示:

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd" />

感谢您的帮助.

推荐答案

如果您要从休眠实体开始生成变更日志,则可以使用

If you want to generate changelog starting with your hibernate entities you can use

mvn liquibase:diff

首先,您需要将方言添加到referenceUrl中(配置中缺少)

first you need to add dialect to your referenceUrl (missing in your configuration)

<referenceUrl>hibernate:spring:ch.hemisoft.immo.domain?hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&amp;dialect=org.hibernate.dialect.H2Dialect</referenceUrl>

这篇关于使用Liquibase和JPA实体生成变更日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 14:43