使用Liquibase和JPA实体生成变更日志
我想将liquibase引入我的项目,首先我想根据我的hibernate实体生成一个changelog文件。
到目前为止,我有:
# 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
<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
我得到如下信息:
// 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
,但是我希望我的第一个变更日志包含初始表的创建。
我也看到了,似乎它试图从空数据库而不是从hibernate类创建一个changelog文件:
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" />
感谢您的帮助。
回答:
如果要从hibernate实体开始生成变更日志,则可以使用
mvn liquibase:diff
首先,您需要将方言添加到referenceUrl中(配置中缺少)
<referenceUrl>hibernate:spring:ch.hemisoft.immo.domain?hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&dialect=org.hibernate.dialect.H2Dialect</referenceUrl>
以上是 使用Liquibase和JPA实体生成变更日志 的全部内容, 来源链接: utcz.com/qa/404810.html