Spring Data:2 个不同数据源之间的关系 [英] Spring Data : relationships between 2 different data sources

查看:25
本文介绍了Spring Data:2 个不同数据源之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个 Spring Boot Application 项目中,我有 2 个数据源:

In a Spring Boot Application project, I have 2 data sources:

  • 一个 MySQL 数据库(又名db1")
  • MongoDB 数据库(又名db2")

我正在使用 Spring Data JPASpring Data MongoDB,而且效果很好……一次一个.

I'm using Spring Data JPA and Spring Data MongoDB, and it's working great... one at a time.

说 db1 处理玩家",而 db2 处理团队"(带有玩家 ID 列表).是否有可能使这两个异构实体之间的关系起作用?(即@ManyToOne、@Transactional、Lazy/Eager 等)

Saying db1 handles "Players", and db2 handles "Teams" (with a list of players' ID). Is it possible to make the relationship between those 2 heterogeneous entities working? (i.e. @ManyToOne, @Transactional, Lazy/Eager, etc.)

例如,我希望能够写:

List<Player> fooPlayers = teamDao.findOneById(foo).getPlayers();

编辑:如果可能,我想找到一个适用于任何 spring 数据项目的解决方案

EDIT: If possible, I'd like to find a solution working with any spring data project

推荐答案

不幸的是,您的难题在 spring 数据中没有解决方案.

Unfortunately your conundrum has no solution in spring data.

可能的情况是您创建自己的接口 (DAO) 类.该 DAO 类将具有查询您的两个数据库的实现.一个非常粗略和简短的例子是

what may be a possibility is that you create an interface (DAO) class of your own. That DAO class would have implementations to to query both of your DBs. A very crude and short example would be

your DAO
{
    yourFind (id)
    {
        this would find in db2 and return a relevant list of objects
        findOneByID(id)
        get the player from the above retrieved list and query db1
        getPlayer(player)
    }
}

我希望这为您指明了正确的方向

i hope this points you in the right direction

这篇关于Spring Data:2 个不同数据源之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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