如何在Java中跨多个不同的SQL数据库创建ORM?

时间:2016-07-22 21:58:45

标签: java hibernate jpa orm eclipselink

是否存在任何基于Java的开源技术,允许我创建对象关系映射,其中底层数据分布在不同的SQL数据库上不同的服务器

重要提示:阅读编写操作应该是可行的。

我调查了hibernate,但无法找到任何有关这种情况是否可能的信息。

实施例

关系数据

DatabaseA.Persons               DatabaseB.ContactDetails
+-------+-------------------+   +----------+-------------------+
| Name  |       Email       |   |  Phone   |       Email       |
+-------+-------------------+   +----------+-------------------+
| John  | john@example.org  |   | 555-0100 | john@example.org  |
| Jenny | jenny@example.org |   | 555-0200 | jenny@example.org |
+-------+-------------------+   +----------+-------------------+

对象关系映射

Class: Persons                   Class: ContactDetails
 + Field: Name                    + Field: Phone
 + Field: Email                   + Field: Email
 + Field: ContactDetails           

 ContactDetailsRelation: Persons.Email -> ContactDetails.Email

虚拟用法

myCustomer.getName()
myCustomer.getEMail()
myCustomer.getContactDetails().getEMail()
myCustomer.getContactDetails().getPhone()

myCustomer.setName("Max")
myCustomer.setEMail("max@example.org")
myCustomer.getContactDetails().setEMail("max@example.org")
myCustomer.getContactDetails().getPhone("555-0300")

myCustomer.setContactDetails(myOtherContactDetails)

1 个答案:

答案 0 :(得分:0)

    **This is exactly what you do.**

 1. You need two hibernate configuration files. You'll need to call them hibernate_sql.cfg.xml and hiberante_oracle.cfg.xml or something like that to differentiate them.
 2. when you initialized your Configuration object, you specifiy the name of the config files. So, you'll have two Configuration objects, from which you'll get two SessionFactory objects.
 3. There you provide the appropriate database connecton config, the dialect to use and which hbm or annotation mapping files for the Entity classes are needed and so on.
 4. within in code you create a single SessionFactory for each database.



    SessionFactory oracleSF = Configuration.configure("hibernate_sql.cfg.xml").buildSessionFfactory();

    SessionFactory msSF = Conf..configure("hiberante_oracle.cfg.xml").build....