简体   繁体   中英

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 , as per the docs , so I have 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 it doesn't 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 expected behaviour and if so what's the correct way to override a property from persistence.xml?

OpenJPA doesn't look at SystemProperties by default when creating an EM/EMF. Try passing in System.getProperties() in when creating your EMF.

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

How are you getting the EntityManager? You can pass properties to the EntityManagerFactory and override persistence.xml that way.

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

In JPA, the values in the standard META-INF/persistence.xml bootstrapping file used by the Persistence class at runtime override the values in the above resource [openjpa.xml] , as well as any System property settings.

I don't know why it's like that, but it's like that.

However, it's also true that:

The Map passed to Persistence.createEntityManagerFactory at runtime also overrides previous settings, including properties defined in persistence.xml.

So if you can get your settings in there, you're good.

In the newer OPENJPA ( 7.0.1 ) in case you want to override a property in the persistence.xml you can pass a system property or in the initial context with as prefix the PU name and then the proprerty to override as suffix.

the 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>

the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM