DDL generation and general persistence.xml settings (OpenJPA)

Summary

I am trying to run a Java JPA 2.0 sample web application. The application was written to run on Glassfish using EclipseLink as a JPA provider. I would like to convert it to run in TomEE with OpenJPA as a JPA provider, but I cannot provide any detailed tutorials for launching and launching using OpenJPA .

Problem

I am having problems with persistence.xml conversion to work with OpenJPA instead of EclipseLink . More specifically, this persistence.xml does not indicate:

  • Entity Are they necessary?
  • Desired JPA Provider. Will the default container be something?
  • JDBC driver. How to specify a β€œbuilt-in” database (for initial testing purposes only)?

also:

  • How are DDL generation properties generated in OpenJPA? I could not find their OpenJPA User Guide .

More details

The following is 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 persistence.xml equivalent will look like for OpenJPA?
  • Alternatively, if anyone can point me to an OpenJPA tutorial that will cover these issues that will be just as good
+8
java jpa openjpa apache-tomee openejb
source share
2 answers

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

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

Alternatively, you can use EclipseLink in TomEE by simply adding EclipseLink banks to <CATALINA_HOME>/lib/

see here General PersistenceProvider Properties

+7
source share

Foreign Key Constraints

The following line does not create foreign keys:

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

Creates only a schema and deletes the contents of the database.

But if you want to 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 SQL output:

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

NOTE To see the generated output in the TomEE console, you need to change the log level in the loggin.properties file using openjpa.level = FINEST


More details at http://openjpa.apache.org/faq.html

+1
source share

All Articles