【发布时间】:2016-01-17 13:00:17
【问题描述】:
如何配置JPA-project (EAR) 和WebSphere Application (8.5.5) 服务器,以便在服务器上启动EAR 时让OpenJPA 创建表?
我在 WAS 中使用默认设置创建了一个数据源(名称:userregister,JNDI:jdbc/userregister)。
在 META-INF/persistence.xml 我有以下内容。
<persistence-unit name="urPU" transaction-type="JTA">
<!-- OpenJPA -->
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>jdbc/userregister</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<shared-cache-mode>DISABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(foreignKeys=true,schemaAction='dropDB,add')" />
<property name="openjpa.jdbc.MappingDefaults" value="ForeignKeyDeleteAction=restrict, JoinForeignKeyDeleteAction=restrict" />
<property name="openjpa.jdbc.SchemaFactory" value="native(foreignKeys=true)" />
<property name="openjpa.Log" value="DefaultLevel=TRACE,SQL=TRACE" />
<property name="openjpa.ConnectionUserName" value="APP" />
<!-- <property name="openjpa.ConnectionPassword" value="APP" /> -->
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
</properties>
</persistence-unit>
如果我通过在 Eclipse 中单击右键选择persistence.xml,我可以在文件中生成类列表。虽然我无法在数据库中手动创建类 => 我认为这应该在部署时发生。
数据库已创建,日志中有 jpa 条目:
The Java Persistence API (JPA) component is initializing.
The com.ibm.websphere.persistence.PersistenceProviderImpl class is loaded as the default Java Persistence API (JPA) provider.
The Java Persistence API (JPA) component has initialized.
...
The Java Persistence API (JPA) component is starting.
The Java Persistence API (JPA) component has started.
...
OpenJPA dynamically loaded a validation provider.
查看日志输出,由于在实际配置 OpenJPA 设置时使用了 IBM 特定的提供程序,是否还有其他一些属性? (如果我理解正确,这两个都应该默认从 WAS 中找到)
注意: 我注意到,如果我将 JNDI 名称从 persistence.xml 更改为无效的名称,我就没有错误。我试图检查 JPA 是否确实与数据源连接。 应该期待什么和/或什么可能是错误的? 从日志来看,JPA 似乎已经开始了。
【问题讨论】:
-
发现:“如果没有在 Enterprise JavaBeans (EJB) 模块中的 persistence.xml 文件的
元素中配置 JPA 提供程序,则当前为此服务器配置的默认 JPA 提供程序用来。”如果我省略了 persistence.xml,则日志中没有 JPA 条目,但是当存在时,该文件似乎被视为“空”? -
从产品随附的提供程序列表中选择默认 JPA 提供程序。打开管理控制台。单击服务器 > 应用程序服务器 选择服务器。单击容器服务 > 默认 Java 持久性 API 设置 选择 选择 WebSphere Application Server 包含的默认持久性提供程序。单击滑块并从列表中选择。单击应用并保存配置。 ...默认提供程序已更改,但问题仍然存在。
-
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>应该足够了。这些表将在第一次请求数据库期间创建。您看到什么样的错误,因为您附加的日志中没有。
标签: jpa openjpa websphere-8