如何在 OpenJPA 中覆盖 persistence.xml 属性
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
创建 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());
恐怕你运气不好。 手册说
我不知道为什么会这样,但就是这样。
然而,这也是事实:
因此,如果您可以在那里进行设置,那就很好了。
I'm afraid you're out of luck. The manual says
I don't know why it's like that, but it's like that.
However, it's also true that:
So if you can get your settings in there, you're good.
你如何获得EntityManager?您可以将属性传递给 EntityManagerFactory 并以这种方式覆盖 persistence.xml。
How are you getting the EntityManager? You can pass properties to the EntityManagerFactory and override persistence.xml that way.
在较新的 OPENJPA ( 7.0.1 ) 中,如果您想要覆盖 persistence.xml 中的属性,您可以传递系统属性或在初始上下文中以 PU 名称作为前缀,然后将要覆盖的属性作为后缀。
原始的 persistence.xml:
覆盖:
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:
the overrides:
http://tomee.apache.org/configuring-persistenceunits-in-tests.html