我如何使用休眠生成迁移?

dan*_*ita 4 java migration hibernate code-first

我想通过 hibernate 和 java 的代码优先方法生成我的数据库。我成功生成了数据库,但我错过了我的迁移文件。hibernate 支持迁移,例如 .NEt 中的 EntityFrameworkCore、PHP 中的 Eloquent 或节点中的 Sequelize?

这是我生成数据库的类:

public class CreateDataBase {
    public static void main(String[] args) {
      Configuration conf = new Configuration();
      conf.configure();

      SchemaExport se = new SchemaExport(conf);
      se.create(true,  true);  
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的架构:

package Infra.HibernateConfiguration;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity 
public class Bank {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id; 

    @Column()
    private String code;

    @Column()
    private String name;

    public int getId() {
        return id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


}

Run Code Online (Sandbox Code Playgroud)

ear*_*dap 9

您可以使用LiquibaseFlyway

Flyway 和 Liquibase 都支持专业数据库重构和版本控制所需的所有功能,因此您将始终知道您正在处理的数据库架构版本以及它是否与您的软件版本匹配。这两个工具都与 Maven 或 Gradle 构建脚本集成,以便您可以完全自动化数据库重构。

  • Flyway不支持生成迁移:https://github.com/flyway/flyway/issues/2667。此外,使用 Liquibase 进行设置几乎是微不足道的。 (2认同)