简体   繁体   English

同一实体的两个Spring存储库-不同的数据源

[英]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 我发现的相关问题是: 使用spring数据jpa使用相同实体/存储库的两个数据源

Also in the solution there it is mentioned to use a 'AbstractRoutingDataSource' which can derive the information from the tenant. 同样在解决方案中,提到使用“ AbstractRoutingDataSource”,它可以从租户派生信息。

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. 如果要同步2个数据库,请使用liquibase API,不要尝试通过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. 您可以创建一个变更集,将数据库中的所有数据吹散到更新中,然后用复制集中的数据填充它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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