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 application am getting error
Description: Field userDataRepo in com.cavion.services.UserDataService required a bean named 'entityManagerFactory' that could not be found. Action: Consider defining a bean named 'entityManagerFactory' in your configuration.
From this Question on StackOverflow helped me to figure out the issue.i need to specify the entityManagerFactoryRef on my JPA repositories .
But i have many repository classes some of them uses Entitymanager 'A' and some of them uses 'B' . my current spring boot application class is like this
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class })
@EnableTransactionManagement
@EntityScan("com.info.entity")
@ComponentScan({"com.info.services","com.info.restcontroller"})
@EnableJpaRepositories("com.info.repositories")
public class CavionApplication {
public static void main(String[] args) {
SpringApplication.run(CavionApplication.class, args);
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}}
I have given the EnableJpaRepositories on the spring boot class , so how can i configure multiple EnableJpaRepositories so that i can configure multiple entityManagerFactory ?
Please suggest the best way to setup the multiple data sources .
In order to let spring knows what
DataSource
is related to whatRepository
you should define it at the@EnableJpaRepositories
annotation. Let's assume that we have two entities, theServers
entity and theDomains
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
):Repository for
Servers
entities (package:org.springdemo.multiple.datasources.repository.servers
)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 thebasePackages
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.Servers
Data Source Configuration: as you can see the basePackages value has the package name of theServers
Repository , and also the values ofentityManagerFactoryRef
andtransactionManagerRef
are different in order to let spring separate each entityManager.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 theServers
Datasource as primary:If you need more information please see the full example for each configuration:
Servers
JPA ConfigurationDomains
JPA ConfigurationIn order to separate each datasource I put the configuration in the
application.properties
file, like this: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