简体   繁体   English

Liquibase 不创建基于 JPA 实体的差异变更日志

[英]Liquibase not creating diff changelog based on JPA Entities

I tried to find the answer in already asked questions as well as in this post that some users recommended: http://www.operatornew.com/2012/11/automatic-db-migration-for-java-web.html but without any luck.我试图在已经提出的问题以及一些用户推荐的这篇文章中找到答案: http : //www.operatornew.com/2012/11/automatic-db-migration-for-java-web.html但没有运气好的话。

The problem is that I have a complete configuration of Liquibase for my Java project with Maven build tool and Postgres DB but even though I have defined Hibernate entities, the Liquibase diff does not take them into consideration and does not produce changelock based on JPA annotated entities.问题是我使用 Maven 构建工具和 Postgres DB 为我的 Java 项目完整配置了 Liquibase,但即使我已经定义了 Hibernate 实体,Liquibase diff也没有考虑它们,并且不会基于 JPA 注释实体生成变更锁.

I tried everything but with empty changelock-master.xml and with defined 2 entities the resulting diff.xml is empty.我尝试了所有方法,但使用空的changelock-master.xml并定义了 2 个实体,结果diff.xml为空。

Here is my pom.xml :这是我的pom.xml

        <!--LIQUIBASE-->
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-maven-plugin</artifactId>
            <version>3.4.1</version>
        </dependency>
        ...
        <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>3.5.3</version>
                <dependencies>
                    <dependency>
                        <groupId>org.liquibase.ext</groupId>
                        <artifactId>liquibase-hibernate4</artifactId>
                        <version>3.6</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-beans</artifactId>
                        <version>4.1.7.RELEASE</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework.data</groupId>
                        <artifactId>spring-data-jpa</artifactId>
                        <version>1.7.3.RELEASE</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <propertyFile>src/main/resources/liquibase.properties</propertyFile>
                    <changeLogFile>src/main/resources/db/changelog/changelog-master.xml</changeLogFile>
                </configuration>
            </plugin>

Here I have my liquibase.properties defined:在这里,我定义了liquibase.properties

referenceUrl=hibernate:spring:com.victus.applied.entity?dialect=org.hibernate.dialect.PostgreSQLDialect
referenceDriver=liquibase.ext.hibernate.database.connection.HibernateDriver
referenceUsername=testusername
referencePassword=
driver=org.postgresql.Driver
url=jdbc:postgresql://localhost:5432/applied
username=testusername
password=
changeLogFile=src/main/resources/db/changelog/changelog-master.xml
diffChangeLogFile=src/main/resources/liquibase-diff.xml
outputChangeLogFile=src/main/resources/db/changelog/changelog-master.xml

In my application.properties I have also:在我的application.properties 中,我还有:

##Liquibase
spring.liquibase.change-log=classpath:/db/changelog/changelog-master.xml
logging.level.liquibase = INFO

##Postgres DB
spring.datasource.url= jdbc:postgresql://localhost:5432/applied
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username=wiktordyngosz
spring.datasource.password=

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.messages.basename=validation

I defined simple two entities, for example one of them:我定义了两个简单的实体,例如其中之一:

@Entity
@Table(name = "user")
@Getter
@Setter
@NoArgsConstructor
@EqualsAndHashCode
public class User {

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

    @NotNull
    private String username;

    @NotNull
    private String password;

    @Transient
    private String passwordConfirm;

    @ManyToMany
    @JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Set<Role> roles;
}

When I have an empty changelock-master.xml file and invoke mvn liquibase:diff to generate changelock based on my entities the resulting changelock is empty.当我有一个空的changelock-master.xml文件并调用mvn liquibase:diff以根据我的实体生成 changelock 时,生成的 changelock 为空。 The log looks like this:日志如下所示:

[INFO] ------------------------------------------------------------------------
[INFO] Parsing Liquibase Properties File
[INFO]   File: src/main/resources/liquibase.properties
[INFO]   'outputChangeLogFile' in properties file is not being used by this task.
[INFO] ------------------------------------------------------------------------
[INFO] Executing on Database: jdbc:postgresql://localhost:5432/applied
INFO 20.02.19 21:19: liquibase-hibernate: Reading hibernate configuration hibernate:spring:com.victus.applied.entity?dialect=org.hibernate.dialect.PostgreSQLDialect
INFO 20.02.19 21:19: liquibase-hibernate: Found package com.victus.applied.entity
INFO 20.02.19 21:19: liquibase-hibernate: Found dialect org.hibernate.dialect.PostgreSQLDialect
INFO 20.02.19 21:19: liquibase-hibernate: Found hibernate.enhanced_idfalse
lut 20, 2019 9:19:26 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
lut 20, 2019 9:19:26 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.11.Final}
lut 20, 2019 9:19:26 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
lut 20, 2019 9:19:26 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
lut 20, 2019 9:19:27 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
lut 20, 2019 9:19:27 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
INFO 20.02.19 21:19: liquibase-hibernate: Using dialect org.hibernate.dialect.PostgreSQLDialect
[INFO] Performing Diff on database wiktordyngosz @ jdbc:postgresql://localhost:5432/applied (Default Schema: public)
INFO 20.02.19 21:19: liquibase: src/main/resources/liquibase-diff.xml exists, appending
[INFO] Differences written to Change Log File, src/main/resources/liquibase-diff.xml
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

The root cause could be that the Liquibase plugin doesn't find your compiled classes on the classpath.根本原因可能是 Liquibase 插件在类路径上找不到您编译的类。

I find it suspicious that you refer to your logfiles by src/main/resources/... .我发现您通过src/main/resources/...引用您的日志文件很可疑。 When executing the Liquibase plugin as part of the Maven build, all resources should be available on class path directly, eg changelog-master.xml (without the relative path).当作为 Maven 构建的一部分执行 Liquibase 插件时,所有资源都应该直接在类路径上可用,例如changelog-master.xml (没有相对路径)。

Try executing the diff goal as part of the Maven build after compiling.编译后尝试将diff目标作为 Maven 构建的一部分执行。 This POM configuration binds the plugin to the process-classes phase :此 POM 配置将插件绑定到process-classes 阶段

<build>
<plugins>
  <plugin>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-maven-plugin</artifactId>
    <version>3.6.3</version>
    <dependencies>
...
    </dependencies>
    <configuration>
      <verbose>true</verbose>
    </configuration>
    <executions>
      <execution>
        <goals>
          <goal>diff</goal>
        </goals>
        <phase>process-classes</phase>
      </execution>
    </executions>
  </plugin>

To execute this, run mvn process-classes or a later phase like mvn test .要执行此操作,请运行mvn process-classes或稍后的阶段,如mvn test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何同时从 JPA 个实体为不同的数据库创建差异更新日志文件? - How to create diff changelog file from JPA entities for different databases at the same time? Hibernate使用JPA(带注释的实体)和liquibase - Hibernate using JPA (annotated Entities) and liquibase JPA使用实体的一部分创建视图 - JPA Creating views with parts of entities Liquibase更新日志不存在-ChangeLogParseException - Liquibase changelog does not exist - ChangeLogParseException Liquibase 双向关系变更日志顺序 - Liquibase bidirectional relationships changelog order Liquibase 找不到更改日志文件 - Liquibase cannot find changelog file 使用Liquibase从现有数据库(包括存储的proc)创建数据库changelog xml文件 - Creating a database changelog xml file from an existing database (including stored procs) using Liquibase 在 Spring 引导中使用 Maven、Liquibase 和 Hibernate 引导生成数据库和实体之间的差异文件会产生错误 - Using Maven, Liquibase and Hibernate in Spring Boot to generate Diff-Files between a Database and Entities yields Error 创建实体并生成数据库表(JSF / JPA) - Creating Entities and Generating Database Tables (JSF / JPA) liquibase changelog文件未从默认值更新 - liquibase changelog file is not updating from default value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM