8

概括

我正在尝试运行 Java Web 应用程序 JPA 2.0 示例。示例应用程序被编写为在 中运行,Glassfish使用EclipseLinkJPA 提供程序。我想将它转换为作为 JPA 提供程序运行TomEEOpenJPA但我无法提供任何详细的教程来启动和运行OpenJPA.

问题

我无法转换persistence.xml为使用OpenJPA而不是EclipseLink. 更具体地说,给定的persistence.xml没有指定:

  • Entity类。这些有必要吗?
  • 所需的 JPA 提供程序。容器会默认为某些东西吗?
  • JDBC 驱动程序。如何指定“内存中”数据库(仅用于初始测试目的)?

还:

细节

下面是 EclipseLink persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="order" transaction-type="JTA">
        <jta-data-source>jdbc/__default</jta-data-source>
        <properties>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
            <property name="eclipselink.ddl-generation.output-mode"
                value="both" />
        </properties>
    </persistence-unit>
</persistence>

我有以下Entity课程:

  • order.entity.LineItem
  • order.entity.LineItemKey
  • order.entity.Order
  • order.entity.Part
  • order.entity.PartKey
  • order.entity.Vendor
  • order.entity.VendorPart

问题

  • 有谁知道 OpenJPA 的等效 persistence.xml 是什么样的?
  • 或者,如果有人可以向我指出一个涵盖这些问题的 OpenJPA 教程,那也一样好
4

2 回答 2

7

如果您添加openjpa.jdbc.SynchronizeMappings如下所示的属性,OpenJPA 将自动创建您的所有表、所有主键和所有外键,以完全匹配您的对象

<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>

或者,您可以在 TomEE 中使用 EclipseLink,只需将 EclipseLink jar 添加到<CATALINA_HOME>/lib/

有关Common PersistenceProvider 属性,请参阅此处

于 2012-05-21T18:51:56.977 回答
1

外键约束

下一行不创建外键:

<property name="openjpa.jdbc.SynchronizeMappings" 
          value="buildSchema(ForeignKeys=true)"/>

仅创建模式并删除数据库的内容。

但如果要创建外键,请使用以下行:

<property name="openjpa.jdbc.SynchronizeMappings" 
          value="buildSchema(foreignKeys=true,schemaAction='dropDB,add')"/>
<property name="openjpa.jdbc.SchemaFactory" 
          value="native(foreignKeys=true)" />
<property name="openjpa.jdbc.MappingDefaults" 
          value="ForeignKeyDeleteAction=restrict, JoinForeignKeyDeleteAction=restrict"/>

查看生成的 SQL

换一种方式,如果你想查看 SQL 输出:

<property name="openjpa.Log" 
          value="DefaultLevel=TRACE,SQL=TRACE" />

注意:为了在 TomEE 控制台中查看生成的输出,您需要使用以下命令更改文件中的日志loggin.properties级别openjpa.level = FINEST


在http://openjpa.apache.org/faq.html中查看更多信息

于 2014-07-10T16:30:12.953 回答