如何通过联接来自Spring Data JPA中不同数据库的2个表来运行本机查询? [英] how to run native query by joining 2 tables from different database in Spring data jpa?

查看:301
本文介绍了如何通过联接来自Spring Data JPA中不同数据库的2个表来运行本机查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot 1.5.18.RELEASE.

I am using Spring boot 1.5.18.RELEASE.

我使用以下链接关注了多个数据源:

I have followed multiple data sources using following link:

但是当我在同一服务器上有数据源时,它可以正常工作.

but its working when I have data sources in same server.

这是我的情况:

    一台服务器上的
  1. datasource1:server1
  2. 另一个服务器上的datasource2:server2
  3. 使用本机查询,我必须通过连接2个数据源来获取记录.
  1. datasource1 at one server: server1
  2. datasource2 at another server: server2
  3. Using native query I have to get the records by joining 2 data sources.

在上述情况下,Spring Boot应用程序仅尝试检查服务器1中的表,结果我得到类似

In above case, spring boot application is trying to check for table in server 1 only and as a result I am getting sql exception something like

SqlExceptionHelper - SQL Error: 1146, SQLState: 42S02

SqlExceptionHelper - Table 'datasource2.table2' doesn't exist

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 
'datasource2.table2' doesn't exist

如何解决此问题?

推荐答案

如果您的表属于同一服务器(不考虑数据库(相同或不同)),则可以相互连接. 但是,如果它们属于不同的服务器,则MySql会将其支持为 FEDERATED Engine .您可以查看此问题以了解更多详细信息.

If your tables are of same server (irrespective of databases (same or differ)) then it is possible to join with eachother. But if they belong different servers then it supports by the MySql as FEDERATED Engine . You can have look to this question for more details.

这篇关于如何通过联接来自Spring Data JPA中不同数据库的2个表来运行本机查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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