Spring Data JPA - Multiple EnableJpaRepositories

前端 未结 3 609
执笔经年
执笔经年 2020-11-29 21:29

My application has multiple data sources , so i have created two data source configuration classes based on this URL .

But while running the spring boot applicatio

相关标签:
3条回答
  • 2020-11-29 21:37

    The answered provided by @Daniel C. is correct. Small correction/observation from my side.

    • @Primary is not required if you don't want to mark any datasource as default one, otherwise necessary.
    • If you are defining any of the EntityManagerFactoryBean with @Bean name as entityManagerFactory then it's better to mark it @Primary to avoid conflict.
    • @ConfigurationProperties("app.datasource.servers") can be marked at class level instead of defining at method level.
    • Better to return HikariDataSource as datasource if you using Spring Boot 2.x or higher version as it has been changed.
    • Make sure you define exact property for jdbc-url which is being used by HikariDataSource to refer JDBC Connection URL.
    0 讨论(0)
  • 2020-11-29 21:38

    I just added a module aware multi database aware library for mysql in github.Some application properties need to be added and you are done .

    Documentation and other details could be found at :-

    https://github.com/yatharthamishra0419/spring-boot-data-multimodule-mysql

    0 讨论(0)
  • 2020-11-29 21:50

    In order to let spring knows what DataSource is related to what Repository you should define it at the @EnableJpaRepositories annotation. Let's assume that we have two entities, the Servers entity and the Domains entity and each one has its own Repo then each Repository has its own JpaDataSource configuration.

    1. Group all the repositories based on the Data Source that they are related to. For example

    Repository for Domains entities (package: org.springdemo.multiple.datasources.repository.domains):

    package org.springdemo.multiple.datasources.repository.domains;
    
    import org.springdemo.multiple.datasources.domain.domains.Domains;
    import org.springframework.data.jpa.repository.JpaRepository;
    
    public interface DomainsRepository extends JpaRepository<Domains,Long> {
    }
    

    Repository for Servers entities (package: org.springdemo.multiple.datasources.repository.servers)

    package org.springdemo.multiple.datasources.repository.servers;
    
    import org.springdemo.multiple.datasources.domain.servers.Servers;
    import org.springframework.data.jpa.repository.JpaRepository;
    
    public interface ServersRepository extends JpaRepository<Servers,Long> {
    }
    

    2. For each JPA Data Soruce you need to define a configuration, in this example I show how to configure two different DataSources

    Domains Jpa Configuration: the relationship between the Data Source and the repository is defined in the basePackages value, that is the reason why is necessary to group the repositories in different packages depending on the entity manager that each repo will use.

    @Configuration
    @EnableJpaRepositories(
            entityManagerFactoryRef = "domainsEntityManager",
            transactionManagerRef = "domainsTransactionManager",
            basePackages = {"org.springdemo.multiple.datasources.repository.domains"}
            )
    public class DomainsConfig {
    

    Servers Data Source Configuration: as you can see the basePackages value has the package name of the Servers Repository , and also the values of entityManagerFactoryRef and transactionManagerRef are different in order to let spring separate each entityManager.

    @Configuration
    @EnableJpaRepositories(
            entityManagerFactoryRef = "serversEntityManager",
            transactionManagerRef = "serversTransactionManager",
            basePackages = {"org.springdemo.multiple.datasources.repository.servers"}
            )
    public class ServersConfig {
    

    3. Set one Datasource as primary

    In order to avoid the error message: Parameter 0 of constructor in org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration required a single bean, but 2 were found: just set one of the datasource as @Primary, in this example I select the Servers Datasource as primary:

    @Bean("serversDataSourceProperties")
    @Primary
    @ConfigurationProperties("app.datasource.servers")
    public DataSourceProperties serversDataSourceProperties(){
        return new DataSourceProperties();
    }
    
    
    
    @Bean("serversDataSource")
    @Primary
    @ConfigurationProperties("app.datasource.servers")
    public DataSource serversDataSource(@Qualifier("serversDataSourceProperties") DataSourceProperties serversDataSourceProperties) {
        return serversDataSourceProperties().initializeDataSourceBuilder().build();
    }
    

    If you need more information please see the full example for each configuration:

    Servers JPA Configuration

    @Configuration
    @EnableJpaRepositories(
            entityManagerFactoryRef = "serversEntityManager",
            transactionManagerRef = "serversTransactionManager",
            basePackages = {"org.springdemo.multiple.datasources.repository.servers"}
            )
    public class ServersConfig {
    
        @Bean(name = "serversEntityManager")
        public LocalContainerEntityManagerFactoryBean getServersEntityManager(EntityManagerFactoryBuilder builder,
                                                                              @Qualifier("serversDataSource") DataSource serversDataSource){
    
    
            return builder
                    .dataSource(serversDataSource)
                    .packages("org.springdemo.multiple.datasources.domain.servers")
                    .persistenceUnit("servers")
                    .properties(additionalJpaProperties())
                    .build();
    
        }
    
        Map<String,?> additionalJpaProperties(){
            Map<String,String> map = new HashMap<>();
    
            map.put("hibernate.hbm2ddl.auto", "create");
            map.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
            map.put("hibernate.show_sql", "true");
    
            return map;
        }
    
    
        @Bean("serversDataSourceProperties")
        @Primary
        @ConfigurationProperties("app.datasource.servers")
        public DataSourceProperties serversDataSourceProperties(){
            return new DataSourceProperties();
        }
    
    
    
        @Bean("serversDataSource")
        @Primary
        @ConfigurationProperties("app.datasource.servers")
        public DataSource serversDataSource(@Qualifier("serversDataSourceProperties") DataSourceProperties serversDataSourceProperties) {
            return serversDataSourceProperties().initializeDataSourceBuilder().build();
        }
    
        @Bean(name = "serversTransactionManager")
        public JpaTransactionManager transactionManager(@Qualifier("serversEntityManager") EntityManagerFactory serversEntityManager){
            JpaTransactionManager transactionManager = new JpaTransactionManager();
            transactionManager.setEntityManagerFactory(serversEntityManager);
    
            return transactionManager;
        }
    }
    

    Domains JPA Configuration

    @Configuration
    @EnableJpaRepositories(
            entityManagerFactoryRef = "domainsEntityManager",
            transactionManagerRef = "domainsTransactionManager",
            basePackages = {"org.springdemo.multiple.datasources.repository.domains"}
            )
    public class DomainsConfig {
    
        @Bean(name = "domainsEntityManager")
        public LocalContainerEntityManagerFactoryBean getdomainsEntityManager(EntityManagerFactoryBuilder builder
        ,@Qualifier("domainsDataSource") DataSource domainsDataSource){
    
            return builder
                    .dataSource(domainsDataSource)
                    .packages("org.springdemo.multiple.datasources.domain.domains")
                    .persistenceUnit("domains")
                    .properties(additionalJpaProperties())
                    .build();
    
        }
    
    
        Map<String,?> additionalJpaProperties(){
            Map<String,String> map = new HashMap<>();
    
            map.put("hibernate.hbm2ddl.auto", "create");
            map.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
            map.put("hibernate.show_sql", "true");
    
            return map;
        }
    
    
        @Bean("domainsDataSourceProperties")
        @ConfigurationProperties("app.datasource.domains")
        public DataSourceProperties domainsDataSourceProperties(){
            return new DataSourceProperties();
        }
    
    
        @Bean("domainsDataSource")
        public DataSource domainsDataSource(@Qualifier("domainsDataSourceProperties") DataSourceProperties domainsDataSourceProperties) {
            return domainsDataSourceProperties.initializeDataSourceBuilder().build();
        }
    
        @Bean(name = "domainsTransactionManager")
        public JpaTransactionManager transactionManager(@Qualifier("domainsEntityManager") EntityManagerFactory domainsEntityManager){
            JpaTransactionManager transactionManager = new JpaTransactionManager();
            transactionManager.setEntityManagerFactory(domainsEntityManager);
    
            return transactionManager;
        }
    
    }
    

    In order to separate each datasource I put the configuration in the application.properties file, like this:

    app.datasource.domains.url=jdbc:h2:mem:~/test
    app.datasource.domains.driver-class-name=org.h2.Driver
    
    
    app.datasource.servers.driver-class-name=com.mysql.jdbc.Driver
    app.datasource.servers.url=jdbc:mysql://localhost:3306/v?autoReconnect=true&useSSL=false
    app.datasource.servers.username=myuser
    app.datasource.servers.password=mypass
    

    If you need more information please see the following documentation:

    Spring Documentation: howto-two-datasources

    A similar example of how configure two different databases: github example

    0 讨论(0)
提交回复
热议问题