How to override persistence.xml properties in OpenJPA

I have the following property in my persistence.xml:

<property name="openjpa.ConnectionProperties" value="DriverClassName=com.mysql.jdbc.Driver,jdbcUrl=jdbc:mysql://localhost:3306/c,user=foo,password=foo,autocommit=false,automaticTestTable=testtable,idleConnectionTestPeriod=60"/> 

I am trying to override it using a system property according to docs , so I have a set:

 -Dopenjpa.ConnectionProperties=DriverClassName=com.mysql.jdbc.Driver,jdbcUrl=jdbc:mysql://localhost:3306/bar,user=bar,password=bar,autocommit=false,automaticTestTable=testtable,idleConnectionTestPeriod=60 

But this does not work : OpenJPA always reads the property value from persistence.xml

Only when the property in persistence.xml is removed does it read the value from the system property.

Is this the expected behavior, and if so, what is the correct way to override a property from persistence.xml?

+4
source share
4 answers

OpenJPA does not by default consider SystemProperties when creating EM / EMF. Try going to System.getProperties () when creating your EMF.

Persistence.createEntityManagerFactory("pu_Name", System.getProperties());

+4
source

How do you get EntityManager? You can pass EntityManagerFactory properties and override persistence.xml this way.

+1
source

I'm afraid you're out of luck. manual says

In JPA, the values ​​in the standard META-INF / persistence.xml bootstrap file used by the Persistence class at run time override the values ​​in the above [openjpa.xml] resource, as well as any system property settings.

I do not know why this is so, but it is.

However, it is also true that:

The map passed to Persistence.createEntityManagerFactory at run time also overrides the previous settings, including the properties defined in the persistence.xml file.

So, if you can get your settings there, you are good.

+1
source

In the new OPENJPA (7.0.1), if you want to override a property in the persistence.xml file, you can pass the system property or in the original context with the PU name prefix, and then proprerty override as a suffix.

original persistence.xml:

 <persistence> <persistence-unit name="movie-unit"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>movieDatabase</jta-data-source> <non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source> <properties> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> <property name="hibernate.max_fetch_depth" value="3"/> </properties> </persistence-unit> </persistence> 

overrides:

 Properties p = new Properties(); p.put(Context.INITIAL_CONTEXT_FACTORY,"org.apache.openejb.client.LocalInitialContextFactory"); p.put("movie-unit.hibernate.hbm2ddl.auto", "update"); p.put("movie-unit.hibernate.dialect", "org.hibernate.dialect.HSQLDialect"); context = new InitialContext(p); 

http://tomee.apache.org/configuring-persistenceunits-in-tests.html

0
source

All Articles