可以覆盖jar依赖项中的persistence.xml中的属性 [英] It is possible to override properties in a persistence.xml that is located in a jar dependency
问题描述
我有一个Java-ee Web应用程序,该应用程序使用一个打包为jar依赖项的持久性单元(实体类,EJB存储库,persistence.xml)。
I have a java-ee web application that uses a persistence unit that is packaged as a jar dependency (Entity classes, EJB repositories, persistence.xml).
为了使Web应用程序运行一些验收测试,我需要覆盖打包的persistence.xml中的一个属性。具体来说,我需要通过设置禁用默认的活动eclipselink 共享对象缓存
In order to get some acceptance tests running for the web application i need to override a property in the packaged persistence.xml. To be specific i need to disable the by default active eclipselink shared object cache by setting the following property.
<property name="eclipselink.cache.shared.default" value="false"/>
这是必要的,因为接受测试是直接使用dbunit准备/清除数据库。这些修改将使eclipselink缓存处于陈旧状态(因为这些修改未涉及持久性单元)。
This is necessary because the acceptance tests are directly prepare/cleanup the database with dbunit. These modifications will put the eclipselink cache in a stale state (because the persistence unit is not involved in these modifications).
java-ee(或glassfish)中是否有办法特定)覆盖位于jar中的persistence.xml中的属性(从运行应用程序时部署的Web应用程序war文件开始)?
Is there a way in java-ee (or glassfish specific) to override properties in a persistence.xml that is located in a jar (starting from the web application war file, that is deployed when running my tests)?
有可能还有其他方法,例如构建特定于测试部署的jar依赖关系,但是对于我来说,仅覆盖我的persistence.xml中的一个属性,这条路线似乎很复杂。
There may be other ways, for example building the jar dependency specific for a test deployment, but this route seems complicated to me for only override one property in my persistence.xml.
推荐答案
您可以将属性映射传递给Persistence.createEntityManagerFactory()。为此,您必须自己管理持久性上下文(无法注入它)。
You can pass a properties map to Persistence.createEntityManagerFactory(). To do this you must manage your persistence context yourself (will not be able to inject it).
另一种选择是将该属性设置为Java系统属性(-D =),这不会覆盖persistence.xml中的现有属性,但是如果该属性不在persistence.xml中,则可以正常工作。
Another option is to set the property as a Java system property (-D=), this will not override an existing property in the persistence.xml, but with work if the property is not in the persistence.xml.
另一个选择是放置您的persistence.xml中的一个SessionCustomizer或SessionTuner,以允许您自己的代码在运行时修改配置。
Another option is to put a SessionCustomizer or a SessionTuner in your persistence.xml to allow your own code to modify the configuration at runtime.
这篇关于可以覆盖jar依赖项中的persistence.xml中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!