问题描述
我正在使用SpringLiquibase在应用程序启动期间自动应用我的liquibase更新.通常,这可以正常工作,但是当我将hibernate.hbm2ddl.auto设置为"validate"时,在liquibase似乎有机会应用更新之前,hibernate开始抱怨数据库方案.我的配置如下:
I'm using SpringLiquibase to apply my liquibase update automatically during the application startup. In general this works fine, but when I set hibernate.hbm2ddl.auto to "validate" then hibernate starts to complain about the database scheme before liquibase seems to have the chance to apply the updates.My configuration looks like this:
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = "com.myapp")
@PropertySource(value = {"classpath:myapp.properties"})
@EnableJpaRepositories("com.myapp")
public class MyappConfig {
@Resource
private Environment env;
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getRequiredProperty("jdbc.driver"));
dataSource.setUrl(env.getRequiredProperty("jdbc.url"));
dataSource.setUsername(env.getRequiredProperty("jdbc.username"));
dataSource.setPassword(env.getRequiredProperty("jdbc.password"));
return dataSource;
}
@Bean
public SpringLiquibase liquibase() {
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setDataSource(dataSource());
liquibase.setChangeLog("classpath:liquibase/liquibase-master-changelog.xml");
return liquibase;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
entityManagerFactoryBean.setPackagesToScan("com.myapp");
entityManagerFactoryBean.setJpaProperties(hibernateProperties());
return entityManagerFactoryBean;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
String[] propertyNames = new String[]{"hibernate.dialect", "hibernate.show_sql", "hibernate.hbm2ddl.auto"};
for (String propertyName : propertyNames) {
properties.put(propertyName, env.getRequiredProperty(propertyName));
}
return properties;
}
@Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
}
在休眠尝试验证架构之前,有没有办法让liquibase应用其更新?
Is there any way to get liquibase to apply its updates BEFORE hibernate tries to validate the schema?
推荐答案
感谢M. Deinum 我能够通过使用
@Bean
@DependsOn("liquibase")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
[...]
}
@DependsOn
确保在休眠模式验证之前运行liquibase.
The @DependsOn
makes sure that liquibase is run before Hibernates schema validation.
这篇关于在休眠之前运行SpringLiquibase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!