持久性单元外部的Kumuluzee JPA持久性设置

问题描述

我们正在尝试配置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>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...