gpt4 book ai didi

java - 如何在 hibernate 验证之前开始 Flyway 迁移?

转载 作者:行者123 更新时间:2023-11-30 02:28:33 27 4
gpt4 key购买 nike

我使用flyway + hibernate 验证。我有飞路 bean :

@Component
public class DbMigration {

private static final Logger LOG = LoggerFactory.getLogger(DbMigration.class);

private final Config config;

@Autowired
public DbMigration(Config config) {
this.config = config;
}

public void runMigration() {
try {
Flyway flyway = new Flyway();
flyway.configure(properties());
int migrationApplied = flyway.migrate();
LOG.info("[" + migrationApplied + "] migrations are applied");
} catch (FlywayException ex) {
throw new DatabaseException("Exception during database migrations: ", ex);
}
}

public Properties properties() {
//my prop
}
}

在应用类中我这样做:

public static void main(String[] args) {
try {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
context.getBean(DbMigration.class).runMigration();

但是我的 hibernate 在 runMigration(); 之前启动并验证抛出异常。接下来我该如何开始?

  1. 运行迁移
  2. 启动 hibernate 验证

编辑:

  @Bean
@Autowired
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource datasource) {
log.info("entityManagerFactory start");
dbMigration.runMigration();

但是我觉得这样不好

最佳答案

在你的 Spring 应用程序配置中,如果你有一个实体管理器工厂 bean 配置,你可以让它依赖于 Flyway bean,以便它在它之后初始化。类似于:

@Bean
@DependsOn("flyway")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
// Initialize EntityManagerFactory here
}

flyway bean 配置可以类似于:

@Bean(initMethod = "migrate")
public Flyway flyway() {
Flyway flyway = new Flyway();
// configure bean here
return flyway;
}

关于java - 如何在 hibernate 验证之前开始 Flyway 迁移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44898850/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com