Spring Boot中的多个数据源和模式创建

时间:2015-02-02 10:20:39

标签: spring hibernate jpa datasource spring-boot

我使用的是Spring Boot。我终于设法设置了两个数据源,但现在我面临另一个问题。

  1. 有两个数据源spring.jpa.hibernate.ddl-auto=create似乎停止在我的春季启动应用程序中工作,只有spring.jpa.generate-ddl=true现在就完成工作

  2. 我无法为每个数据源选择自动创建策略。我更愿意为数据源1创建模式,只使用第二个数据库中创建的模式和数据源2。

  3. 任何机构都知道如何解决这些问题?注意我不想在可能的情况下完全丢弃自动配置。我还不知道,如果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;
            }
        }
    

    M.W。

1 个答案:

答案 0 :(得分:23)

spring.jpa.hibernate.ddl-auto=create已停止工作,不是因为您有两个DataSource,而是因为您的应用程序正在创建自己的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();
}