gpt4 book ai didi

java - 对一个实体使用两个实体管理器

转载 作者:行者123 更新时间:2023-12-01 21:49:28 27 4
gpt4 key购买 nike

情况如下:我有两个数据库Db1Db2,为此我有两个EntityManagerem1em2定义。此外,我将实体 Person(int id, String name, Pet pet) 映射到数据库 Db1 中的表 Person(id, name) 和实体 Pet( int id, String name, Person Owner) 映射到数据库 db2 中的表 pets(id, name, person_id)。 Person 和 Pet 之间的关系是 @OneToOne 关系。在程序的某个时刻,我想做这样的事情:

PersonDAO personDAO = db1DAOFactory.getPersonDAO();
Person person = personDAO.find(100);
System.out.println(person.getPet().getName());

不可能合并两个数据库。我如何告诉 Hibernate 将 EntityManager em2 用于宠物领域?对于 Hibernate,我只使用注释,没有 xml 配置。

非常感谢!

最佳答案

抱歉,但我认为你不能。 nakosspy 回答了这个问题:

Hibernate will create an SQL query for your HQL or Criteria query and this SQL will be sent through jdbc to the database. This means that hibernate does not support for what you are trying to do.

However, you can achieve the same result in some cases. Some databases give you the option to create an alias for a table that resides in a different database. So you will be able to write an SQL query that joins the two tables and execute it on the database.

We are doing that with DB2. If you can do that, it depends on your database.

I guess, that it would impossible if you have two different databases (example DB2 and MySQL) but if both databases are of the same vendor, then maybe it's achievable.

You should try to find more info in you database server's documentation.

如果您想查看他的原始帖子,请查看此 link .

关于java - 对一个实体使用两个实体管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35398966/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com