问题描述
我们正在尝试配置Kumuluz JPA。
我们想以编程方式定制Persistence Unit,为此,我们需要PersistenceUnit Properties的句柄。这已经预先包装在kumuluz jpa依赖项中,并且我们显然无法在运行时获取属性的句柄。
有人在必须在运行时设置属性时遇到同样的问题吗?你能分享你的方法吗?
解决方法
您无法在运行时中访问persistence.xml配置,也没有任何意义,因为JPA提供程序仅在应用程序启动的最开始就读取persistence.xml。
但是,您可以使用Maven Resources Plugin在Maven配置的构建期间配置persistence.xml。例如:
pom.xml:
<project>
...
<properties>
<db.action>create</db.action>
</properties>
...
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
...
</project>
persistence.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="kumuluzee-samples-jpa" transaction-type="JTA">
<jta-data-source>jdbc/CustomersDS</jta-data-source>
<class>com.kumuluz.ee.samples.jpa.Customer</class>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="${db.action}"/>
</properties>
</persistence-unit>
</persistence>