Schema Creation

使用 SQL 数据库时,模式是基本组成部分。Spring Data JDBC 支持广泛的模式选项,但是在从域模型开始时,提出一个初始域模型可能会具有挑战性。为了在代码优先方法中提供帮助,Spring Data JDBC 随附与集成以使用 Liquibase 创建数据库更改集。

When working with SQL databases, the schema is an essential part. Spring Data JDBC supports a wide range of schema options yet when starting with a domain model it can be challenging to come up with an initial domain model. To assist you with a code-first approach, Spring Data JDBC ships with an integration to create database change sets using Liquibase.

考虑以下域实体:

Consider the following domain entity:

@Table
class Person {
  @Id long id;
  String firstName;
  String lastName;
  LocalDate birthday;
  boolean active;
}

通过以下代码呈现初始 ChangeSet:

Rendering the initial ChangeSet through the following code:

RelationalMappingContext context = … // The context contains the Person entity, ideally initialized through initialEntitySet
LiquibaseChangeSetWriter writer = new LiquibaseChangeSetWriter(context);

writer.writeChangeSet(new FileSystemResource(new File(…)));

产生以下更改日志:

yields the following change log:

databaseChangeLog:
- changeSet:
    id: '1685969572426'
    author: Spring Data Relational
    objectQuotingStrategy: LEGACY
    changes:
    - createTable:
        columns:
        - column:
            autoIncrement: true
            constraints:
              nullable: false
              primaryKey: true
            name: id
            type: BIGINT
        - column:
            constraints:
              nullable: true
            name: first_name
            type: VARCHAR(255 BYTE)
        - column:
            constraints:
              nullable: true
            name: last_name
            type: VARCHAR(255 BYTE)
        - column:
            constraints:
              nullable: true
            name: birthday
            type: DATE
        - column:
            constraints:
              nullable: false
            name: active
            type: TINYINT
        tableName: person

列类型由实现 SqlTypeMapping 策略界面的对象计算。可空性由类型推断,如果一个属性类型使用原始 Java 类型,则将其设置为 false

Column types are computed from an object implementing the SqlTypeMapping strategy interface. Nullability is inferred from the type and set to false if a property type use primitive Java types.

架构支持可以在整个应用开发生命周期中为你提供帮助。在差分模式中,你会向架构编写器实例提供一个现有的 Liquibase Database,架构编写器会将现有表与映射实体进行比较,并从差异推导出要创建/删除哪些表和列。默认情况下,不会删除任何表和列,除非你配置 dropTableFilterdropColumnFilter。两个过滤器谓词都提供表名或列名,因此你的代码可以计算可以删除哪些表和列。

Schema support can assist you throughout the application development lifecycle. In differential mode, you provide an existing Liquibase Database to the schema writer instance and the schema writer compares existing tables to mapped entities and derives from the difference which tables and columns to create/to drop. By default, no tables and no columns are dropped unless you configure dropTableFilter and dropColumnFilter. Both filter predicate provide the table name respective column name so your code can computer which tables and columns can be dropped.

writer.setDropTableFilter(tableName -> …);
writer.setDropColumnFilter((tableName, columnName) -> …);

架构支持只能标识表/列的添加和删除,即删除未映射或数据库中不存在的表/列。列无法重命名,也无法迁移数据,因为实体映射未提供架构如何演变的详细信息。

Schema support can only identify additions and removals in the sense of removing tables/columns that are not mapped or adding columns that do not exist in the database. Columns cannot be renamed nor data cannot be migrated because entity mapping does not provide details of how the schema has evolved.