I want to use two databases in spring boot application and each databases contains 20 tables.
I referred below solutions:
- https://www.baeldung.com/spring-data-jpa-multiple-databases
- https://www.javadevjournal.com/spring-boot/multiple-data-sources-with-spring-boot/
- Spring Boot Configure and Use Two DataSources
- Spring Boot, Spring Data JPA with multiple DataSources
Note: in all above solutions performed configuration only for two tables. such as below:
1. Customer Configuration:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "customerEntityManagerFactory",
transactionManagerRef = "customerTransactionManager",
basePackages = {
"com.javadevjournal.customer.repo"
}
)
public class CustomerConfig {
@Primary
@Bean(name = "customerDataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource customerDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "customerEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean
entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("customerDataSource") DataSource dataSource
) {
return builder
.dataSource(dataSource)
.packages("com.javadevjournal.customer.data")
.persistenceUnit("db1")
.build();
}
@Primary
@Bean(name = "customerTransactionManager")
public PlatformTransactionManager customerTransactionManager(
@Qualifier("customerEntityManagerFactory") EntityManagerFactory customerEntityManagerFactory
) {
return new JpaTransactionManager(customerEntityManagerFactory);
}
}
2. Product Configuration
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "productEntityManagerFactory",
transactionManagerRef = "productTransactionManager",
basePackages = {
"com.javadevjournal.product.repo"
}
)
public class ProductConfig {
@Bean(name = "productDataSource")
@ConfigurationProperties(prefix = "db2.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "productEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean
barEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("productDataSource") DataSource dataSource
) {
return
builder
.dataSource(dataSource)
.packages("com.javadevjournal.product.data")
.persistenceUnit("db2")
.build();
}
@Bean(name = "productTransactionManager")
public PlatformTransactionManager productTransactionManager(
@Qualifier("productEntityManagerFactory") EntityManagerFactory productEntityManagerFactory
) {
return new JpaTransactionManager(productEntityManagerFactory);
}
}
- Created a Customer Repository by Extending JpaReposity<Customer,Integer>
- Created a Product Repository by Extending JpaReposity<Product,Integer>
Questions:
- what if my each database has 20 tables? how to configure them like above? do we need to include package in which all those entities reside
- do we need to create all 20 repositories for those entities by extending Jparepository (i think this is not good), then how can we do that?
- if i don't want to use JpaRepository, then how can configure Entitymanager per Datasource?