0

We have a need to choose a JPA provider (Hibernate or EclipseLink) at run time based on a system property.

From what I have understood, the provider name must be given in the persistence.xml and can not be overridden in code. If no provider name is given, the first provider in the classpath is used by default. Another alternative could have been to use 2 different persistence.xml and load one of them based on the value of the system property. However, this doesn't seem to be possible either (JPA using alternative "persistence.xml").

Is there any other way to do this ?

Community
  • 1
  • 1
Rahul
  • 12,886
  • 13
  • 57
  • 62

2 Answers2

1

Turns out there is an easy way to do this by defining multiple persistence units in the persistence.xml each with a different provider name.

Then at run time, pass the correct persistence unit name while creating the EntityManagerFactory.

Rahul
  • 12,886
  • 13
  • 57
  • 62
0

Another way is to access the persistence provider directly, instead of through Persistence.

James
  • 17,965
  • 11
  • 91
  • 146