1

How to have named native queries for 2 databases and have the EclipseLink run the right one on the db?

I give an example: I have a solution that can run with Oracle Db and PostgreSQL. There is a need for native queries to be run on db. So the native query for the Oracle is different to the one for PostgresSQL. How can I have them in the orm.xml as named queries and then have EclipseLink run the correct one on each db based on the run time environment.

Should there be alternative orm files for each database? No stored procedure/db function and similar stuff is allowed. So I have to have named native quires.

Ali
  • 899
  • 2
  • 13
  • 33

1 Answers1

2

JPA 2.1 allows adding named queries dynamically to a factory, allowing you to specify the query you want to add at runtime based on your platform.

Unless you are going to pass the target database to your code, getting it from the provider will require native API or using a connection to get its metadata. Getting it from EclipseLink would require getting the session and then calling getPlatform() on it and use its isOracle and isPostgreSQL to determine which query to add, or a generic one for other platforms. Something like

entityManagerFactory.unwrap(Session.class).getPlatform().isOracle();
Chris
  • 20,138
  • 2
  • 29
  • 43