I am trying to learn how to use multiple Datasources in Spring Boot and the examples I am seeing are so complex. From “Spring Boot” autoconfiguration you now suddenly have to manually configure everything: you have to define packages with Entities and Packages with Repositories and you have to manually create TransactionManager & EntityManagerFactoryBuilder.
So now I have to constantly update Package Paths for
- basePackages in @EnableJpaRepositories
- packages() in return
entityManagerFactoryBuilder
and these can’t be refactored automatically if I decide to rearange Classes.
Is there a simpler way? Like defining different Datasources and then using Annotations on Entities to specify where they should be saved.
Or maybe even manually configuring EntityManager for every Datasource but then using Annotations on Entities to specify which EntityManager should be used.
And can I avoid separately specifying Packages for Entities & repositories? Suddenly there is so much overhead configuration.
Here is the example Application I am currently using
https://github.com/ivoronline/springboot_db_datasource_SaveDifferentEntitiesInDifferentSchemas
Schema1Config.java
@Configuration
@EnableJpaRepositories(
basePackages = "com.ivoronline.springboot_db_datasource_diffentitiesdiffschemas.schema1.repository",
entityManagerFactoryRef = "schema1EntityManagerFactoryBean",
transactionManagerRef = "schema1TransactionManager"
)
public class Schema1Config {
//=========================================================================================================
// DATA SOURCE PROPERTIES
//=========================================================================================================
@Bean
@ConfigurationProperties("schema1.spring.datasource")
public DataSourceProperties schema1DataSourceProperties() {
return new DataSourceProperties();
}
//=========================================================================================================
// DATA SOURCE
//=========================================================================================================
@Bean
@Primary
public DataSource schema1DataSource() {
return schema1DataSourceProperties().initializeDataSourceBuilder().build();
}
//=========================================================================================================
// ENTITY MANAGER FACTORY BEAN
//=========================================================================================================
@Bean
LocalContainerEntityManagerFactoryBean schema1EntityManagerFactoryBean (
EntityManagerFactoryBuilder entityManagerFactoryBuilder,
DataSource dataSource
) {
return entityManagerFactoryBuilder
.dataSource(dataSource)
.packages("com.ivoronline.springboot_db_datasource_diffentitiesdiffschemas.schema1.entity")
.build();
}
//=========================================================================================================
// SCHEMA 1 TRANSACTION MANAGER
//=========================================================================================================
@Bean
PlatformTransactionManager schema1TransactionManager(@Qualifier("schema1EntityManagerFactoryBean") LocalContainerEntityManagerFactoryBean emfb) {
return new JpaTransactionManager(emfb.getObject());
}
}