Configure Relational and Non-Relational databases in the same project with Spring-Data + Hibernate

2

I'm trying to create a Java application where I need to set up two databases: MongoDB and MySQL. My idea is to use Spring-Data with Hibernate for both banks, but I found only tutorials for setting up Cross-Store between the banks.

Is there any way I can configure my templates so that each one is persisted in the database that I set up in XML?

    
asked by anonymous 08.04.2015 / 00:06

1 answer

1

Using class-based and annotation-based configuration, you can split the configuration of each Spring Data JPA repository and its DataSource into a separate configuration file.

This article has an example:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "barEntityManagerFactory", 
        transactionManagerRef = "barTransactionManager",
        basePackages = { "com.sctrcd.multidsdemo.integration.repositories.bar" })
public class BarConfig {

    @Autowired JpaVendorAdapter jpaVendorAdapter;

    @Bean(name = "barDataSource")
    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder()
                .setName("bardb").setType(EmbeddedDatabaseType.HSQL).build();
    }

    @Bean(name = "barEntityManager")
    public EntityManager entityManager() {
        return entityManagerFactory().createEntityManager();
    }

    ...

}

Then you would do the same for FooConfig , that is, for the other configuration class that will set another DataSource , EntityManager and @EnableJpaRepositories .

Via XML the process would be identical. The important thing is that the EntityManager and TransactionManager especificados em cada configuração de repositório apontem para o DataSource 'desired.

    
08.04.2015 / 21:42