Is it possible to do cross database table joins in 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.
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) 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