我正在使用Spring Boot。我终于设法设置了两个数据源,但是现在我面临另一个问题。
有两个数据源的地方spring.jpa.hibernate.ddl-auto=create似乎无法在我的Spring Boot应用程序spring.jpa.generate-ddl=true中工作,请立即执行此操作
spring.jpa.hibernate.ddl-auto=create
spring.jpa.generate-ddl=true
我无法为每个数据源选择自动创建策略。我宁愿为数据源一创建模式,而仅在第二个DB中将数据源为二使用创建的模式。
任何机构都知道如何解决这些问题?注意如果可能的话,我不想完全放弃自动配置。我什至还不知道,hibernate是否能够在一个持久性单元中初始化架构。
application.properties
spring.datasource-internal.url=jdbc:hsqldb:mem:testdb spring.datasource-internal.username=sa spring.datasource-internal.password=sa spring.datasource-internal.driver-class-name=org.hsqldb.jdbcDriver spring.datasource-internal.jpa.database-platform=org.hibernate.dialect.HSQLDialect spring.datasource-external.url=jdbc:hsqldb:mem:testexternal spring.datasource-external.username=sa spring.datasource-external.password=sa spring.datasource-external.driver-class-name=org.hsqldb.jdbcDriver spring.datasource-external.jpa.database-platform=org.hibernate.dialect.HSQLDialect flyway.enabled=false spring.jpa.hibernate.ddl-auto=create spring.jpa.show-sql=true spring.jpa.generate-ddl=true
DBInternalConfig
@Configuration @EnableTransactionManagement @EnableJpaRepositories(basePackages = "cz.data.internal", entityManagerFactoryRef = "internalEntityManagerFactory", transactionManagerRef = "internalTransactionManager") public class DBConfigInternal { public static final String INTERNAL = "internal"; @Bean(name = "internalDataSource") @Primary @ConfigurationProperties(prefix = "spring.datasource-internal") public DataSource internalDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "internalEntityManagerFactory") @Primary public LocalContainerEntityManagerFactoryBean internalEntityManagerFactory( EntityManagerFactoryBuilder builder) { return builder .dataSource(internalDataSource()) .packages("cz.data.internal.entity") .persistenceUnit(INTERNAL) .build(); } @Bean(name = "internalTransactionManager") @Primary public PlatformTransactionManager internalTransactionManager() { JpaTransactionManager jpaTransactionManager = new JpaTransactionManager(); jpaTransactionManager.setDataSource(internalDataSource()); jpaTransactionManager.setPersistenceUnitName(INTERNAL); return jpaTransactionManager; } }
DBExternalConfig
@Configuration @EnableTransactionManagement @EnableJpaRepositories( basePackages = "cz.data.external", entityManagerFactoryRef = "externalEntityManagerFactory", transactionManagerRef = "externalTransactionManager") public class DBConfigExternal { public static final String EXTERNAL = "external"; @Bean(name = "externalDataSource") @ConfigurationProperties(prefix = "spring.datasource-external") public DataSource externalDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "externalEntityManagerFactory") public LocalContainerEntityManagerFactoryBean externalEntityManagerFactory( EntityManagerFactoryBuilder builder) { return builder .dataSource(externalDataSource()) .packages("cz.data.external.entity") .persistenceUnit(EXTERNAL) .build(); } @Bean(name = "externalTransactionManager") public PlatformTransactionManager externalTransactionManager() { JpaTransactionManager jpaTransactionManager = new JpaTransactionManager(); jpaTransactionManager.setDataSource(externalDataSource()); jpaTransactionManager.setPersistenceUnitName(EXTERNAL); return jpaTransactionManager; } }
spring.jpa.hibernate.ddl-auto=create之所以停止工作,不是因为你有两个DataSource,而是因为你的应用程序正在创建自己LocalContainerEntityManagerFactoryBean的。这具有禁用a的自动配置的效果,LocalContainerEntityManagerFactoryBean因此你现在必须自己配置它。
LocalContainerEntityManagerFactoryBean
你可以将两个实体管理器配置为具有不同的架构生成行为,如下所示(第一个进行更新,第二个进行创建):
@Bean(name = "externalEntityManagerFactory") public LocalContainerEntityManagerFactoryBean externalEntityManagerFactory( EntityManagerFactoryBuilder builder) { Map<String, Object> properties = new HashMap<String, Object>(); properties.put("hibernate.hbm2ddl.auto", "update"); return builder .dataSource(externalDataSource()) .packages("cz.data.external.entity") .persistenceUnit(EXTERNAL) .properties(properties) .build(); } @Bean(name = "internalEntityManagerFactory") @Primary public LocalContainerEntityManagerFactoryBean internalEntityManagerFactory( EntityManagerFactoryBuilder builder) { Map<String, Object> properties = new HashMap<String, Object>(); properties.put("hibernate.hbm2ddl.auto", "create"); return builder .dataSource(internalDataSource()) .packages("cz.data.internal.entity") .persistenceUnit(INTERNAL) .properties(properties) .build(); }