如何在 OpenJPA 中覆盖 persistence.xml 属性

发布于 2024-11-04 18:08:24 字数 885 浏览 3 评论 0原文

我的 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"/>

我正在尝试使用系统属性覆盖它,按照 docs,所以我设置了:

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

但它不起作用: OpenJPA 始终从 persistence.xml 中读取属性值

仅当 persistence.xml 中的属性被删除时,它才会从系统属性中读取值。

这是预期的行为吗?如果是的话,从 persistence.xml 覆盖属性的正确方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南巷近海 2024-11-11 18:08:24

创建 EM/EMF 时,OpenJPA 默认情况下不会查看 SystemProperties。尝试在创建 EMF 时传入 System.getProperties()。

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

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());

私藏温柔 2024-11-11 18:08:24

恐怕你运气不好。 手册

在 JPA 中,Persistence 类在运行时使用的标准 META-INF/persistence.xml 引导文件中的值会覆盖上述资源中的值[openjpa.xml],以及任何系统属性设置。

我不知道为什么会这样,但就是这样。

然而,这也是事实:

在运行时传递给 Persistence.createEntityManagerFactory 的 Map 还会覆盖以前的设置,包括 persistence.xml 中定义的属性。

因此,如果您可以在那里进行设置,那就很好了。

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.

生生漫 2024-11-11 18:08:24

你如何获得EntityManager?您可以将属性传递给 EntityManagerFactory 并以这种方式覆盖 persistence.xml。

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

∞梦里开花 2024-11-11 18:08:24

在较新的 OPENJPA ( 7.0.1 ) 中,如果您想要覆盖 persistence.xml 中的属性,您可以传递系统属性或在初始上下文中以 PU 名称作为前缀,然后将要覆盖的属性作为后缀。

原始的 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>

覆盖:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文