1

我最近开始了一个使用 Maven、Hibernate 和 Liquibase 的 MySQL 数据库的项目,我对这些都没有经验,所以请原谅我的幼稚。

我现在的目标是尝试生成一个差异更改日志,我假设它是一个更改日志,显示正常 changelog.xml 和 hbm.xml 之间的更改

我在 hbm 中插入了一个新属性 int age,并希望 liquibase 能够检测到年龄不包含在更改日志中,然后会生成一个差异更改日志,说明有一个额外的属性或类似的东西。然而,它产生了一个空的变更日志,并说没有变化。

以下是我项目的一些部分,但我只包括了那些似乎与 liquibase-maven:diff 插件相关的部分:

用户.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;
    }

}

用户.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>

更改日志.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

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

<?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>

运行 liquibase-maven:diff 插件后,我的输出如下:

输出

"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    

由此产生的 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"/>

如您所见,接近尾声的输出说:

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

尽管在 hbm 中添加了一个额外的属性。

显然我做错了什么,它没有检测到变化,我试图多次查看 liquibase-hibernate 文档,但现在我被卡住了。如果我的帖子看起来很大并且我的代码看起来很乱,我很抱歉,我还在学习。这也是我第一次在stackoverflow上发帖,请见谅。谢谢!

PS如果我的项目的其他部分有需要,我会按要求提交。

4

2 回答 2

1

您以错误的方式配置了数据库。referenceUrl应该是hibernate:hibernate.cfg.xmlurl应该是jdbc:mysql://localhost:3306/test

于 2013-11-05T15:42:48.277 回答
0

您的数据流在这里都是错误的。

Hibernate 获取它在数据库中找到的内容,并在.hbm.xml文件的帮助下生成 POJO。

Liquibase 获取变更日志并将其应用于数据库。

所以为了改变你的数据库,你:

  1. 为您的更改添加更改日志条目并对其进行测试
  2. 更新您的.hbm.xml文件以反映更改日志中的更改。

现在您将看到 liquibase 应用您的更改,因为它在更改日志中。Liquibase 根本不知道也不关心.hbm.xml文件中的内容。该文件仅用于休眠创建 POJO。

于 2013-08-28T19:40:15.760 回答