我有一个带有两个数据源的spring boot应用程序。我的主要来源是带有休眠和liquibase的postgres。我的第二个数据源是postgres,但没有liquibase。我有两个实体包装。但是,当我运行mvn liquibase:diff
时,变更日志不仅包含来自de.example.model
的主要实体,而且还包含来自de.example.secondary.model
的实体,这仅适用于辅助数据源。我不知道如何排除这些实体。这是我的主要配置:
@Profile("!test")
@Configuration
@EnableJpaRepositories(basePackages = { "de.example.repository",
"de.example.other.repository" }, entityManagerFactoryRef = "entityManager", transactionManagerRef = "transactionManager")
public class DatabaseConfig {
@Autowired
private Environment env;
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource DataSource() {
return DataSourceBuilder.create().build();
}
@Bean("entityManager")
@Primary
public LocalContainerEntityManagerFactoryBean EntityManager(EntityManagerFactoryBuilder builder) {
return builder.dataSource(DataSource()).persistenceUnit("entityManager")
.packages("de.example.model").properties(hibernateProperties()).build();
}
private Map<String, Object> hibernateProperties() {
Map<String, Object> properties = new HashMap<>();
properties.put("hibernate.physical_naming_strategy", env.getProperty("spring.jpa.hibernate.naming.physical-strategy"));
properties.put("hibernate.hbm2ddl.auto", env.getProperty("spring.jpa.hibernate.ddl-auto"));
properties.put("hibernate.dialect", env.getProperty("spring.jpa.properties.hibernate.dialect"));
return properties;
}
@Bean("transactionManager")
@Primary
public PlatformTransactionManager transactionManager(
@Qualifier("entityManager") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
@Bean
@ConfigurationProperties(prefix = "spring.liquibase")
public LiquibaseProperties LiquibaseProperties() {
return new LiquibaseProperties();
}
@Bean("liquibase")
public SpringLiquibase Liquibase() {
return springLiquibase(DataSource(), LiquibaseProperties());
}
private static SpringLiquibase springLiquibase(DataSource dataSource, LiquibaseProperties properties) {
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setDataSource(dataSource);
liquibase.setChangeLog(properties.getChangeLog());
liquibase.setContexts(properties.getContexts());
liquibase.setDefaultSchema(properties.getDefaultSchema());
liquibase.setDropFirst(properties.isDropFirst());
liquibase.setShouldRun(properties.isEnabled());
liquibase.setLabels(properties.getLabels());
liquibase.setChangeLogParameters(properties.getParameters());
liquibase.setRollbackFile(properties.getRollbackFile());
return liquibase;
}
}