简体   繁体   中英

Two Spring Repositories for same entity - Different Datasources

I have a scenario where I have a Database with some records in it already. I need to replicate the information in other database , with the datasource information derived in the run time.

The entity model remains the same in both the databases and all I need is to migrate the information from one to another.

Related question I found is this : Two datasources using the same entities/repositories using spring data jpa

Also in the solution there it is mentioned to use a 'AbstractRoutingDataSource' which can derive the information from the tenant.

My use case does not fit there exactly, or I do not know how to fit it in.

My requirement is like

DataSource1 -> EntityManager1 -> Persistence Unit1.
DataSoruce2 -> EntityManager2 -> Persistence Unit2.

Both the persistence unit has the same set of classes.

I need to do

EntityRepo_associated_with_datasource1.find()  
 and then write them to  EntityRepo_associated_with_datasource2.saveAll().

Any pointers on this ?

If you want to syncronise 2 databases use the liquibase API do not try and do this with JPA.

You can create a changeset that blows away all the data in the database to by updated, then fills it with data from the replicated set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM