Kumuluzee JPA持久化单元外的持久化设置



我们正在尝试配置Kumuluz JPA。

我们想以编程方式定制PersistenceUnit,为此,我们需要一个PersistenceUnitProperties的句柄。这已经预先打包在kumuluz-jpa依赖项中,我们显然无法在运行时获得属性的句柄。

有没有人遇到过同样的问题,必须在运行时设置属性?你能分享一下你的方法吗?

您无法在运行时访问persistence.xml配置,这样做也没有任何意义,因为JPA提供程序仅在应用程序启动之初读取persistence.xml。

但是,您可以使用maven资源插件在构建时使用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>

最新更新