跨数据库加入JPA [英] Cross database joins in JPA
问题描述
是否可以在JPA中执行交叉数据库表连接?
Is it possible to do cross database table joins in JPA?
我在一个数据库中有一个 users
表,其中有一个组织的外键
在单独的数据库中。这两个数据库都在同一物理机器上。现在MySQL允许我写跨越多个数据库的查询,但我不知道如何用JPA这样做。
I have a users
table in one database which has a foreign key to a organizations
table in a separate database. Both the databases are on same physical machine. Now MySQL allows me to write queries which span across multiple databases, but I am not sure how to do this with JPA.
@Entity
Java POJO的注释没有使用数据库的名称,因此没有办法标记交叉数据库关系。
The @Entity
annotations on the Java POJO's don't take the name of the database so there is no way to mark a cross DB relationship.
这种情况的解决方法?也许使用本机查询来加载已加入的实体?
Is there a workaround for this situation? Perhaps using a native query to load the joined entity?
推荐答案
我们尝试了以下方法,似乎工作。
We tried out the following approach and seems to work.
1)@Table注释中没有架构属性
1) No schema attribute in the @Table annotations
2)为实体创建不同的orm文件它们所在的模式。
2) create different orm files for entities clubbed by the schema in which they are present.
3)在每个orm文件中,您可以添加my_schema。
3) In each of the orm files, you can add a "my_schema".
4)在你的相应的PU中的orm文件在persistence.xml
4) Include the orm files in your respective PUs in the persistence.xml
5)如果你想在测试期间不同的数据库,创建类似的orm文件测试和更改模式并将这些orm文件包含在单独的PU中
5) And if you want different databases during tests, create similar orm files for test and change the value in the schema accordingly and include these orm files in a separate PU
HTH
这篇关于跨数据库加入JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!