hibernate - Two datasources using the same entities/repositories using spring data jpa -


i need setting application copyies subset of data our in-house (master) ms sql database db1 mysql database db2 runs on webserver used webapplication (i.e. want sync 2 databases). tables have same name on both databases, although subset of attributes/columns defined on db2. i'd use (xml-configured) spring, spring data jpa (i.e. jparepository interfaces) , hibernate jpa-provider, because want build knowledge these tools.

as far pick other discussions, basic idea define spring use 2 entitymanagerfactory beans using 1 distinc datasource each (one each database) according to:

spring jpa hibernate more datasources

i inject persistence-unit-specific entitymanagers , database-operations using these. i'd use spring-data repositories: @ moment have defined jpa entity incl. fields tables/attributes need on db2. each entity, i've set @repository-annotated interface impements jparepository, these repositories injected corresponding service-classes.

is there way inject these repositories servce layer assign persistence-unit (i.e. select database want use) them during injection? in principle define repositories twice, once each database usign @persistencecontext(name="nameofpersistenceunit") annotation, hoped not necessary.

could outline how achive goal using named frameworks, or there easier ways copy data between 2 databases programmatically. appreciated.

looks similar issue want ability point different databases @ run time want keep 1 set of repositories. see if thread helps.. spring data + jpa multiple datasources 1 set of repositories