使用弹簧数据jpa使用相同实体/存储库的两个数据源 [英] Two datasources using the same entities/repositories using spring data jpa

查看:97
本文介绍了使用弹簧数据jpa使用相同实体/存储库的两个数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助建立一个应用程序,它将我们内部(主)MS SQL数据库DB1中的一部分数据复制到另一个MySQL数据库DB2,该数据库在Web服务器上运行以供Web应用程序使用(即,我想同步那两个数据库)。这两个表在两个数据库上都有相同的名称,但在DB2上只定义了属性/列的子集。我希望使用(xml配置的)spring,spring data jpa(即JpaRepository接口)和hibernate作为jpa-provider,因为我想用这些工具建立知识。



就我所能从其他讨论中获得的内容而言,其基本思想是将spring定义为使用两个entityManagerFactory bean,每个bean使用一个distinc dataSource(每个数据库一个)根据以下内容:



spring jpa hibernate with more datasources



然后,我可以注入持久化单元特定的EntityManagers并使用这些操作执行数据库操作。但我想使用spring-data存储库:目前我已经定义了一个JPA Entity incl。字段,用于DB2所需的所有表/属性。对于每个实体,我设置了一个@ Repository注释接口来阻止JpaRepository,然后将这些存储库注入到相应的Service类中。



有没有办法将这些存储库注入到我的servce层,并在注入期间为它们分配一个持久性单元(即选择我想使用的数据库)?我原则上可以定义所有存储库两次,每个数据库一次使用@PersistenceContext(name =NameOfPersistenceUnit)注释,但我希望这不是必需的。



有人可以概述我如何使用命名的框架来实现我的目标,或者有更简单的方法以编程方式在两个数据库之间复制数据。任何帮助表示赞赏。

解决方案

看起来非常类似于您希望在运行时指向不同数据库的问题,但希望只保留一组存储库。看看这个线程是否有所帮助..春天,春天,春天,春天,春天,春天,春天,春天,春天,春天,春天,春天,春天,春天,春天Data + JPA具有多个数据源,但只有一套知识库

I need help setting up an application which copyies a subset of data from our in-house (master) MS SQL database DB1 to another MySQL database DB2 which runs on a webserver to be used by an webapplication (i.e. I want to sync those two databases). The tables have the same name on both databases, although only a subset of the attributes/columns are defined on DB2. I'd like to use (xml-configured) spring, spring data jpa (i.e. the JpaRepository interfaces) and hibernate as jpa-provider, because I want to build up knowledge with these tools.

As far as I could pick up from other discussions, the basic idea is to define spring to use two entityManagerFactory beans using one distinc dataSource each (one for each database) according to:

spring jpa hibernate with more datasources

I could then inject the persistence-unit-specific EntityManagers and do database-operations using these. But I'd like to use the spring-data repositories: At the moment I have defined a JPA Entity incl. fields for all the tables/attributes I need on DB2. For each Entity, I've set up an @Repository-annotated interface which impements JpaRepository, these repositories are then injected into corresponding Service-classes.

Is there any way to inject these repositories into my servce layer an assign a persistence-unit (i.e. select the database I want to use) to them during injection? I could in principle define all repositories twice, once for each database usign the @PersistenceContext(name="NameOfPersistenceUnit") annotation, but I hoped that this would not be necessary.

Could someone outline how I could achive my goal using the named frameworks, or are there easier ways to copy data between two databases programmatically. Any help is appreciated.

解决方案

Looks very similar to issue where you want ability to point to different databases at run time but want to keep only one set of repositories. See if this thread helps.. Spring Data + JPA with multiple datasources but only one set of Repositories

这篇关于使用弹簧数据jpa使用相同实体/存储库的两个数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆