使用 liquibase 设置 spring 引导项目
Setting up spring boot project with liquibase
我有一个 spring boot 2 项目 (jhipster),我对 hibernate tables 如何与 liquibase 一起工作有点困惑。
现在我有一堆数据 类 带有休眠注释,我想插入一些静态数据用于测试目的。我在开始的过程中遇到了一堆问题。
当我已经有了休眠注释数据 类 时,是否必须定义 liquibase changeSets
来创建 table?
如何运行 liquibase changeSets
配置数据库?
编辑 -----
变更集和域(java)代码之间的外键关系如何命名?例如,我有一个 Person
table,它包含对 Address
table 的引用。我如何在 changeSet table 中表示这种关系?
Person
table 不包含 Address
的 ID,它包含引用。
Spring boot has excellent integration with liquibase.
如果您希望 liquibase 处理 tables (DDL) 的创建而不是休眠:
您需要禁用休眠自动创建标志。
设置spring.jpa.hibernate.ddl-auto=none
(或)从 application.yml 文件中删除此 属性。
只需将以下内容添加到 pom.xml 中:
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
下面是创建 table 并将数据插入 table 的示例代码。
File location:
src/main/resources/db/changelog/db.changelog-master.yaml
databaseChangeLog:
- changeSet:
id: 1
author: sgollapinni
changes:
- createTable:
tableName: person
columns:
- column:
name: id
type: int
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: first_name
type: varchar(255)
constraints:
nullable: false
- column:
name: last_name
type: varchar(255)
constraints:
nullable: false
- createTable:
tableName: address
columns:
- column:
name: id
type: int
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: city
type: varchar(255)
constraints:
nullable: false
- column:
name: person_id
type: varchar(255)
constraints:
nullable: false
foreignKeyName: fk_person_address
references: person(id)
- changeSet:
id: 2
author: sgollapinni
changes:
- insert:
tableName: person
columns:
- column:
name: first_name
value: Sunil
- column:
name: last_name
value: Kumar
- insert:
tableName: address
columns:
- column:
name: city
value: Bangalore
- column:
name: user_id
value: (Select id from person where name = 'Sunil')
否则,如果您希望 hibernate 处理 DDL,并且只想插入一些静态数据用于测试目的,您仍然可以使用 liquibase 来完成此操作。
您可以使用 changeSets 添加 DML 语句。
- changeSet:
id: 1
author: sgollapinni
changes:
- insert:
tableName: person
columns:
- column:
name: first_name
value: Sunil
- column:
name: last_name
value: Kumar
- insert:
tableName: address
columns:
- column:
name: city
value: Bangalore
- column:
name: user_id
value: (Select id from person where name = 'Sunil')
希望对您有所帮助!
我有一个 spring boot 2 项目 (jhipster),我对 hibernate tables 如何与 liquibase 一起工作有点困惑。
现在我有一堆数据 类 带有休眠注释,我想插入一些静态数据用于测试目的。我在开始的过程中遇到了一堆问题。
当我已经有了休眠注释数据 类 时,是否必须定义 liquibase changeSets
来创建 table?
如何运行 liquibase changeSets
配置数据库?
编辑 -----
变更集和域(java)代码之间的外键关系如何命名?例如,我有一个 Person
table,它包含对 Address
table 的引用。我如何在 changeSet table 中表示这种关系?
Person
table 不包含 Address
的 ID,它包含引用。
Spring boot has excellent integration with liquibase.
如果您希望 liquibase 处理 tables (DDL) 的创建而不是休眠:
您需要禁用休眠自动创建标志。
设置spring.jpa.hibernate.ddl-auto=none
(或)从 application.yml 文件中删除此 属性。
只需将以下内容添加到 pom.xml 中:
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
下面是创建 table 并将数据插入 table 的示例代码。
File location: src/main/resources/db/changelog/db.changelog-master.yaml
databaseChangeLog:
- changeSet:
id: 1
author: sgollapinni
changes:
- createTable:
tableName: person
columns:
- column:
name: id
type: int
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: first_name
type: varchar(255)
constraints:
nullable: false
- column:
name: last_name
type: varchar(255)
constraints:
nullable: false
- createTable:
tableName: address
columns:
- column:
name: id
type: int
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: city
type: varchar(255)
constraints:
nullable: false
- column:
name: person_id
type: varchar(255)
constraints:
nullable: false
foreignKeyName: fk_person_address
references: person(id)
- changeSet:
id: 2
author: sgollapinni
changes:
- insert:
tableName: person
columns:
- column:
name: first_name
value: Sunil
- column:
name: last_name
value: Kumar
- insert:
tableName: address
columns:
- column:
name: city
value: Bangalore
- column:
name: user_id
value: (Select id from person where name = 'Sunil')
否则,如果您希望 hibernate 处理 DDL,并且只想插入一些静态数据用于测试目的,您仍然可以使用 liquibase 来完成此操作。
您可以使用 changeSets 添加 DML 语句。
- changeSet:
id: 1
author: sgollapinni
changes:
- insert:
tableName: person
columns:
- column:
name: first_name
value: Sunil
- column:
name: last_name
value: Kumar
- insert:
tableName: address
columns:
- column:
name: city
value: Bangalore
- column:
name: user_id
value: (Select id from person where name = 'Sunil')
希望对您有所帮助!