DDL generation and general persistence.xml settings (OpenJPA)
Asked Answered
A

2

8

Summary

I'm trying to run a Java web application JPA 2.0 example. The example application was written to run in Glassfish, using EclipseLink as JPA provider. I would like to convert it to run in TomEE with OpenJPA as the JPA provider, but I can't any detailed tutorials for getting up and running with OpenJPA.

Problem

I'm having trouble converting persistence.xml to work with OpenJPA instead of EclipseLink. More specifically, the given persistence.xml doesn't specify:

  • Entity classes. Are these necessary?
  • The desired JPA provider. Will the container default to something?
  • The JDBC driver. How do I specify an "in-memory" DB (just for initial testing purposes)?

Also:

  • How are the DDL generation properties expressed in OpenJPA? I wasn't able to find them the OpenJPA User Guide.

Details

Below is the 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>

I have the following Entity classes:

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

Question

  • Does anyone know what the equivalent persistence.xml would look like for OpenJPA?
  • Alternatively, if anyone could point me to an OpenJPA tutorial that covers these issues that would be just as good
Andromache answered 20/5, 2012 at 23:3 Comment(3)
Have you spent any time reading the OpenJPA user manual?Littoral
If you have a specific link, that would be swell. I'm going through these now: openjpa.apache.org/samples.htmlAndromache
I haven't been able to find any persistence.xml properties for DDL generation, in here: openjpa.apache.org/builds/2.2.0/apache-openjpa/docs/manual.htmlAndromache
S
7

If you add the openjpa.jdbc.SynchronizeMappings property as shown below OpenJPA will auto-create all your tables, all your primary keys and all foreign keys exactly to match your objects

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

Alternatively, you can use EclipseLink in TomEE by just adding the EclipseLink jars to <CATALINA_HOME>/lib/

refer here for Common PersistenceProvider properties

Saros answered 21/5, 2012 at 18:51 Comment(4)
Thanks. I downloaded EclipseLink, but am not sure which .jar files I need. I found the following jar files in EclipseLink/jlib/jpa/ : ´eclipselink-jpa-modelgen_2.3.2.v20111125-r10461.jar´, ´javax.persistence_1.0.0.jar´, ´javax.persistence_2.0.3.v201010191057.jar´. Are these the only files I need to copy into lib/?Andromache
This is a new question I guess, I'll create another one. Thanks for your answerAndromache
You tell what to create table but i must know that how can i prevent openjpa trying to create tables?Something like:<property name="openjpa.jdbc.SynchronizeMappings" value="false"/>Affectional
Simply removing the openjpa.jdbc.SynchronizeMappings property should do it.Saros
E
1

Foreign key constraints

The next line does not create foreign keys:

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

Only creates schema and deletes content of a database.

But if you want create foreign keys, use the following lines:

<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"/>

See generated SQL

In another way, if you want to see the SQL output:

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

NOTE: In order to see the generated output in the TomEE console, you need to change the log level in the file loggin.properties with openjpa.level = FINEST


See more in http://openjpa.apache.org/faq.html

Egression answered 10/7, 2014 at 16:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.