简体   繁体   English

Liquibase-maven插件未检测到Hibernate hbm.xml和changelog.xml之间的更改

[英]Liquibase-maven plugin doesn't detect change between Hibernate hbm.xml and changelog.xml

I recently started on a project that utilizes MySQL database with Maven, Hibernate, and Liquibase, and I am not at all experienced in any of these so please forgive my naivety. 我最近开始了一个将MySQL数据库与Maven,Hibernate和Liquibase结合使用的项目,而我在这些方面都没有任何经验,请原谅我。

My goal for right now is trying to produce a diff changelog which I assume is a changelog that shows the changes between the normal changelog.xml and the hbm.xml 我现在的目标是尝试生成一个差异更改日志,我假设它是一个更改日志,显示了正常changelog.xml和hbm.xml之间的更改。

I inserted a new property, int age, into the hbm and was hoping that liquibase would detect that age is not included in the changelog, which would then produce a diff changelog saying there is an extra property or something like that. 我在hbm中插入了一个新属性int age,希望liquibase可以检测到该变更日志中未包含年龄,然后会生成一个diff变更日志,表示存在其他属性或类似内容。 However it produced a changelog that was empty and said there was no changes. 但是,它生成的更改日志为空,并表示没有更改。

The following are some of the parts of my project, but I only included those that seem relevant to the liquibase-maven:diff plugin: 以下是我项目的某些部分,但我只包括了那些与liquibase-maven:diff插件相关的部分:

User.java User.java

package com.jnguyen.project;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class User extends CustomizableEntity{

    @Id
    private int id;

    private String username;
    private String firstname;
    private String lastname;
    private int age;   //new property which doesn't exist in changelog.xml yet

    public User() {}

    public void createUsername(String firstname, String lastname, int id) {
        this.username = firstname.substring(0,2).toLowerCase() + lastname.substring(0,2).toLowerCase() + String.format("%05d", id);
    }

    public void setAge(int age)
    {
        this.age = age;
    }

    public int getAge() {
        return this.age;
    }
    public void setUsername(String username) {
        this.username = username;
    }

    public String getUsername() {
        return this.username;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getFirstname() {
        return this.firstname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getLastname() {
        return this.lastname;
    }

    private void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return this.id;
    }

}

User.hbm.xml User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping auto-import="true" package="com.jnguyen.project">

    <class name="User" table="users">
        <id name="id" column="id">
            <generator class="increment"/>
        </id>
        <property name="firstname"/>
        <property name="lastname"/>
        <property name="username"/>
        <property name="age"/>

        <dynamic-component insert="true" name="customProperties" optimistic-lock="true" unique="false" update="true">
        </dynamic-component>
    </class>

</hibernate-mapping>

changelog.xml changelog.xml

<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">

    <changeSet id="1.0_1" author="jnguyen">
        <createTable tableName="users">
            <column name="id" type="int(5)"/>
            <column name="firstname" type="varchar(20)"/>
            <column name="lastname" type="varchar(20)"/>
            <column name="username" type="varchar(20)"/>

        </createTable>
        <addPrimaryKey columnNames="id"
                       tableName="users"/>
        <addAutoIncrement
                columnDataType="int(5) UNSIGNED ZEROFILL"
                columnName="id"
                incrementBy="1"
                startWith="1"
                tableName="users"/>

    </changeSet>

</databaseChangeLog>

liquibase.properties liquibase.properties

promptOnNonLocalDatabase=false
changeLogFile=db/ddl/changelog.xml
url:hibernate:hibernate.cfg.xml
referenceDriver:com.mysql.jdbc.Driver
referenceUrl:jdbc:mysql://localhost:3306/test
referenceUsername:root
referencePassword:rootpass

pom.xml 的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>

    <groupId>com.jnguyen.project</groupId>
    <artifactId>jnguyen.project</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>Hibernate Project</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.codehaus.grepo</groupId>
            <artifactId>grepo-query-hibernate</artifactId>
            <version>1.5.3</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>

        <dependency>
            <groupId>teamdev</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.5.8</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>

        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0-rev-1</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.mojo.hibernate3</groupId>
            <artifactId>maven-hibernate3</artifactId>
            <version>2.2</version>
            <type>pom</type>
        </dependency>

        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
            <version>3.0.0-rc2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>com.springsource.org.hibernate.core</artifactId>
            <version>4.1.0.Final</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.5.6</version>
        </dependency>

        <!-- Spring AOP dependency -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>org.apache.commons.logging</artifactId>
            <version>1.0.4.v200706111724</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>net.customware.liquibase</groupId>
            <artifactId>liquibase-hibernate</artifactId>
            <version>2.1.0</version>
        </dependency>

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

    </dependencies>

<build>
        <plugins>
            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>2.0.5</version>
                <configuration>
                    <diffChangeLogFile>db/ddl/diffchangelog.xml</diffChangeLogFile>
                    <changeLogFile>db/ddl/changelog.xml</changeLogFile>
                    <propertyFile>db/liquibase.properties</propertyFile>
                    <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
                    <driver>com.mysql.jdbc.Driver</driver>
                    <url>jdbc:mysql://localhost:3306/test</url>
                    <username>root</username>
                    <password>rootpass</password>
                    <referenceDriver>com.mysql.jdbc.Driver</referenceDriver>
                    <referenceUrl>jdbc:mysql://localhost:3306/test</referenceUrl>
                    <referenceUsername>root</referenceUsername>
                    <referencePassword>rootpass</referencePassword>
                    <migrationSqlOutputFile>script.sql</migrationSqlOutputFile>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

My output would be the following after I run the liquibase-maven:diff plugin: 运行liquibase-maven:diff插件后,输出为以下内容:

Output 产量

"C:\Program Files\Java\jdk1.7.0_25\bin\java" "-Dmaven.home=C:\Program Files\apache-maven-3.0.5" "-Dclassworlds.conf=C:\Program Files\apache-maven-3.0.5\bin\m2.conf" -Didea.launcher.port=7545 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 12.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\apache-maven-3.0.5\boot\plexus-classworlds-2.4.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 12.1.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher --fail-fast --strict-checksums org.liquibase:liquibase-maven-plugin:2.0.5:diff
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Hibernate Project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- liquibase-maven-plugin:2.0.5:diff (default-cli) @ jnguyen.project ---
[INFO] ------------------------------------------------------------------------
[INFO] Parsing Liquibase Properties File
[INFO]   File: db/liquibase.properties
[INFO] ------------------------------------------------------------------------
[INFO] Executing on Database: jdbc:mysql://localhost:3306/test
[INFO] Performing Diff on database root@localhost @ jdbc:mysql://localhost:3306/test
INFO 6/20/13 12:47 PM:liquibase: Reading tables for root@localhost @ jdbc:mysql://localhost:3306/test ...
INFO 6/20/13 12:47 PM:liquibase: Reading views for root@localhost @ jdbc:mysql://localhost:3306/test ...
INFO 6/20/13 12:47 PM:liquibase: Reading foreign keys for root@localhost @ jdbc:mysql://localhost:3306/test ...
INFO 6/20/13 12:47 PM:liquibase: Reading primary keys for root@localhost @ jdbc:mysql://localhost:3306/test ...
INFO 6/20/13 12:47 PM:liquibase: Reading columns for root@localhost @ jdbc:mysql://localhost:3306/test ...
INFO 6/20/13 12:47 PM:liquibase: Reading unique constraints for root@localhost @ jdbc:mysql://localhost:3306/test ...
INFO 6/20/13 12:47 PM:liquibase: Reading indexes for root@localhost @ jdbc:mysql://localhost:3306/test ...
INFO 6/20/13 12:47 PM:liquibase: Sequences not supported for root@localhost @ jdbc:mysql://localhost:3306/test ...
INFO 6/20/13 12:47 PM:liquibase: Reading tables for root@localhost @ jdbc:mysql://localhost:3306/test ...
INFO 6/20/13 12:47 PM:liquibase: Reading views for root@localhost @ jdbc:mysql://localhost:3306/test ...
INFO 6/20/13 12:47 PM:liquibase: Reading foreign keys for root@localhost @ jdbc:mysql://localhost:3306/test ...
INFO 6/20/13 12:47 PM:liquibase: Reading primary keys for root@localhost @ jdbc:mysql://localhost:3306/test ...
INFO 6/20/13 12:47 PM:liquibase: Reading columns for root@localhost @ jdbc:mysql://localhost:3306/test ...
INFO 6/20/13 12:47 PM:liquibase: Reading unique constraints for root@localhost @ jdbc:mysql://localhost:3306/test ...
INFO 6/20/13 12:47 PM:liquibase: Reading indexes for root@localhost @ jdbc:mysql://localhost:3306/test ...
INFO 6/20/13 12:47 PM:liquibase: Sequences not supported for root@localhost @ jdbc:mysql://localhost:3306/test ...
INFO 6/20/13 12:47 PM:liquibase: db\ddl\diffchangelog.xml exists, appending
INFO 6/20/13 12:47 PM:liquibase: No changes found, nothing to do
[INFO] Differences written to Change Log File, db/ddl/diffchangelog.xml
INFO 6/20/13 12:47 PM:liquibase: Successfully released change log lock
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.566s
[INFO] Finished at: Thu Jun 20 12:47:17 CDT 2013
[INFO] Final Memory: 8M/243M
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0    

The diffchangelog.xml that was produced out of this: 由此产生的diffchangelog.xml:

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

As you can see, the output near the end said: 如您所见,末尾的输出显示:

INFO 6/20/13 12:47 PM:liquibase: No changes found, nothing to do

despite adding an extra property into the hbm. 尽管在hbm中添加了额外的属性。

Clearly I am doing something wrong that it is not detecting the change, and I've tried to look over the liquibase-hibernate documentation multiple times but now I am stuck. 显然,我做错了一些事情,因为它没有检测到更改,因此我尝试多次查看liquibase-hibernate文档,但现在我陷入了困境。 I am sorry if my post seem large and if my code seems messy, I'm still learning. 很抱歉,如果我的帖子看起来很大,并且我的代码看起来很凌乱,我仍在学习。 This is my also first time posting on stackoverflow, so forgive me please. 这也是我第一次在stackoverflow上发帖,请原谅。 Thank you! 谢谢!

PS if there is a need for the other parts of my project, I will submit them on request. PS:如果需要我项目的其他部分,我将根据要求提交。

You've configured the databases the wrong way round. 您以错误的方式配置了数据库。 referenceUrl should be hibernate:hibernate.cfg.xml and url should be jdbc:mysql://localhost:3306/test . referenceUrl应该是hibernate:hibernate.cfg.xmlurl应该是jdbc:mysql://localhost:3306/test

The flow of your data is all wrong here. 您的数据流在这里是错误的。

Hibernate takes what it has found on the database and generates POJOs with the help of the .hbm.xml file. Hibernate利用它在数据库中找到的内容,并在.hbm.xml文件的帮助下生成POJO。

Liquibase takes a changelog and applies it to a database. Liquibase接收一个变更日志并将其应用于数据库。

So in order to change your database, you: 因此,为了更改数据库,您:

  1. Add a changelog entry for your change and test it 添加更改日志条目以进行更改并进行测试
  2. Update your .hbm.xml file to reflect the changes in the changelog. 更新您的.hbm.xml文件以反映更改日志中的更改。

Now you will see liquibase applying your change since it is in the changelog. 现在,您将看到liquibase应用您的更改,因为它在更改日志中。 Liquibase does not know or care at all about what is in the .hbm.xml file. Liquibase完全不知道或不在乎.hbm.xml文件中的内容。 That file is just for hibernate to create POJOs. 该文件仅用于休眠以创建POJO。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM