I have two data source configuration files that successfully allow my spring app to connect to a snowflake database and another mysql database. I want to have two JpaRepositories for the two databases but both JpaRepositories are pointing to the mysql database. What is the way to use JPA for two different databases? I can't really find any good info online
Asked
Active
Viewed 140 times
0
-
Possible duplicate https://stackoverflow.com/questions/26308035/spring-boot-spring-data-jpa-with-multiple-datasources – Mark B Sep 03 '21 at 17:24
1 Answers
1
You can annotate each configuration file like this :
@EnableJpaRepositories(
entityManagerFactoryRef = "snowFlakeEntityManager",
transactionManagerRef = "snowFlakeTransactionManager")
here you have specified a name to one JpaRepo
for snowflake db
and you can do the same with mysql
.
Then in Repository class you can annotate the entity manager with the name you have specified before in configuration file like that :
@PersistenceContext
@Qualifier("snowFlakeEntityManager")
EntityManager snowFlakeEntityManager;

Mohammed Elnwam
- 31
- 3