Cross database joins in JPA

眉间皱痕 提交于 2019-12-03 16:27:40

We tried out the following approach and seems to work.

1) No schema attribute in the @Table annotations

2) create different orm files for entities clubbed by the schema in which they are present.

3) In each of the orm files, you can add a "my_schema".

4) Include the orm files in your respective PUs in the persistence.xml

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

You can't. As each entity is bound to an persistance context and the context is bound to a database.

If by databases you mean schemas on the same server you can do 2 things

  • create a view on one of the schemas, pointing to the table on the other schema. The downside, is that you might need to map an entity twice (once for each schema)
  • Create a view with the join and map any values you need from there. The downside is that the entity will be read only.

If both schemas are on different databases, then you'll have to do the join manually in your code.

One quesion for you. The "foreign key" you mentioned, is a real DB foreign key or a logical FK ?

If MySQL allows you to write SQL that query across the database, then you can use this SQL in a native Query in JPA.

I assume you are using some kind of database linking mechanism? If so, then you should be able to map this as well. You can set the "schema" on your @Table of the linked database to the link name.

i.e.

@Table(name="organizations", schema="org_schema@org_db")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!