使用Hibernate在不同数据库中的2个表上进行连接

时间:2022-05-20 08:29:07

I have two tables in two separate Oracle databases (not schemas) that I need to join in Hibernate. Currently, I have two Hibernate sessions going out to the separate databases. Before anybody says look at Hibernate Shards, I have spent a better part of a day looking at that sub-project and have found that: it is for horizontal partitioned data (all the tables must be in all of the databases AFAIK), there is no way for one to tell Shards to look only in one database (Hibernate Shards Docs), and is no longer being worked on.

我需要在Hibernate中加入两个独立的Oracle数据库(不是模式)中的两个表。目前,我有两个Hibernate会话进入单独的数据库。在任何人看到Hibernate Shards之前,我花了一天时间看着那个子项目并且发现:它是针对水平分区数据(所有表必须在所有数据库AFAIK中),有没有办法告诉Shards只能在一个数据库中查看(Hibernate Shards Docs),而且不再使用它。

Things that I have thought about to try to solve this problem:

我曾想过试图解决这个问题的事情:

  • Doing a findAll() or some restricted variant of that on both of the tables, and manually doing the join using some loops. (Ok for very small tables - prohibitive from small tables on up)

    在两个表上执行findAll()或某些受限制的变体,并使用一些循环手动执行连接。 (适用于非常小的桌子 - 禁止从小桌子上来)

  • Have the sessions do some kind of interaction (I have no idea if this is even feasible - will have to look at the Hibernate Session API)

    让会话进行某种交互(我不知道这是否可行 - 将不得不查看Hibernate Session API)

  • Removing the database name from the URL string of different hibernate-xxxx.cfg.xml and insert them into the separate hbm.xml files like this:
    <class name="foo" table="foo_table" schema="foo_schema" catalog="foo_db">
    (Doesn't seem to work from my initial tests and that seems like truck sized security hole)

    从不同hibernate-xxxx.cfg.xml的URL字符串中删除数据库名称,并将它们插入到单独的hbm.xml文件中,如下所示: (似乎从我的初始测试中看起来不起作用,而且看起来像卡车大小的安全漏洞)

  • Use the Repository Pattern (Unsure if my Java-Fu is strong enough)

    使用存储库模式(如果我的Java-Fu足够强大,则不确定)

Is there something that I'm overlooking in one of the cases above or can it be another way that I haven't listed above?

在上面的一个案例中是否有一些我忽略的东西,或者它是否是我未在上面列出的另一种方式?

2 个解决方案

#1


3  

You have a couple of problems unfortunately.

不幸的是,你有几个问题。

  • Hibernate does not supporting joining across multiple "physical" database instances
  • Hibernate不支持跨多个“物理”数据库实例加入
  • Out of the box, most database don't support joining across multiple "physical" database instances
  • 开箱即用,大多数数据库不支持跨多个“物理”数据库实例加入

Fundamentally DBs are only good/performant at joining tables that are in the same database. There are ways of joining across databases but if the size of both tables is big this can be a problem, and peformance may suffer. Do some googling on "oracle join across database" and you'll find some pointers on how to do this but it involves fiddling around with Oracle create a virtual link from one DB to the other.

从根本上说,DB在连接同一数据库中的表时只有良好/高效。有两种方法可以跨数据库连接,但如果两个表的大小很大,这可能是一个问题,并且性能可能会受到影响。做一些关于“跨越数据库的oracle连接”的谷歌搜索,你会发现一些关于如何做到这一点的指针,但它涉及摆弄Oracle创建从一个数据库到另一个数据库的虚拟链接。

I would consider either doing the join in memory, if you are comfortable the data set will fit within the memory constraints AND you are only doing this in one special case.

我会考虑在内存中进行连接,如果您觉得数据集符合内存约束,那么您只能在一个特殊情况下执行此操作。

If you'll need to do different joins between these two databases then I would go for a more permanent solution, like the Oracle linking above.

如果您需要在这两个数据库之间进行不同的连接,那么我会寻求更持久的解决方案,例如上面的Oracle链接。

#2


0  

I have no experience with doing this myself, but I know that Oracle supports database "links" between two separate database instances. Maybe this article will help you?

我自己没有这方面的经验,但我知道Oracle支持两个独立数据库实例之间的数据库“链接”。也许这篇文章会对你有帮助吗?

Post on Hibernate forums about using Oracle link between two instances

在Hibernate论坛上发布关于在两个实例之间使用Oracle链接的帖子

#1


3  

You have a couple of problems unfortunately.

不幸的是,你有几个问题。

  • Hibernate does not supporting joining across multiple "physical" database instances
  • Hibernate不支持跨多个“物理”数据库实例加入
  • Out of the box, most database don't support joining across multiple "physical" database instances
  • 开箱即用,大多数数据库不支持跨多个“物理”数据库实例加入

Fundamentally DBs are only good/performant at joining tables that are in the same database. There are ways of joining across databases but if the size of both tables is big this can be a problem, and peformance may suffer. Do some googling on "oracle join across database" and you'll find some pointers on how to do this but it involves fiddling around with Oracle create a virtual link from one DB to the other.

从根本上说,DB在连接同一数据库中的表时只有良好/高效。有两种方法可以跨数据库连接,但如果两个表的大小很大,这可能是一个问题,并且性能可能会受到影响。做一些关于“跨越数据库的oracle连接”的谷歌搜索,你会发现一些关于如何做到这一点的指针,但它涉及摆弄Oracle创建从一个数据库到另一个数据库的虚拟链接。

I would consider either doing the join in memory, if you are comfortable the data set will fit within the memory constraints AND you are only doing this in one special case.

我会考虑在内存中进行连接,如果您觉得数据集符合内存约束,那么您只能在一个特殊情况下执行此操作。

If you'll need to do different joins between these two databases then I would go for a more permanent solution, like the Oracle linking above.

如果您需要在这两个数据库之间进行不同的连接,那么我会寻求更持久的解决方案,例如上面的Oracle链接。

#2


0  

I have no experience with doing this myself, but I know that Oracle supports database "links" between two separate database instances. Maybe this article will help you?

我自己没有这方面的经验,但我知道Oracle支持两个独立数据库实例之间的数据库“链接”。也许这篇文章会对你有帮助吗?

Post on Hibernate forums about using Oracle link between two instances

在Hibernate论坛上发布关于在两个实例之间使用Oracle链接的帖子