Developing JPA 2.x applications for a Java SE environment
Prepare persistence applications to test outside of the application server container in a Java™ SE environment.
About this task
Attention: When you use these JPA command tools, run them from the profile_root/bin directory,
rather than from the app_server_root/bin directory to make sure that you
have the latest version of the commands for your release level.
For this task, you must specify the com.ibm.ws.jpa-2.1.thinclient_9.0.jar stand-alone Java archive (JAR) file in your classpath for compiling with JPA 2.1 interfaces. To compile with JPA 2.0 interfaces, specify the com.ibm.ws.jpa-2.0.thinclient_9.0.jar in your class path. This stand-alone JAR file is available from the installation images. The location of these files on the server installation image is in the ${app_server_root}/runtimes/ directory.
Important: Java Persistence API (JPA) applications
require different configuration techniques from applications that use container-managed persistence
(CMP) or bean-managed persistence (BMP). They do not follow the typical deployment techniques that
are associated with applications that implement CMP or BMP. In JPA applications, you must define a
persistence unit and configure the appropriate properties in the
persistence.xml file to ensure that the applications can run in a Java SE environment.
There are some considerations for running JPA applications in a Java SE environment:
- Resource injection is not available. You must configure these services specifically or programmatically.
- The life cycle of the EntityManagerFactory and EntityManager are managed by the application. Applications control the creation, manipulation, and deletion of these constructs programmatically.
Procedure
Example
The following is a sample persistence.xml file for the Java SE Environment:
For EclipseLink:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="TheWildZooPU" transaction-type="RESOURCE_LOCAL">
<!-- additional Mapping file, in addition to orm.xml>
<mapping-file>META-INF/JPAorm.xml</mapping-file>
<class>com.company.bean.jpa.PersistebleObjectImpl</class>
<class>com.company.bean.jpa.Animal</class>
<class>com.company.bean.jpa.Dog</class>
<class>com.company.bean.jpa.Cat</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.url"
value="jdbc:derby:target/database/jpa-test-database;create=true" />
<property name="javax.persistence.schema-generation.database.action"
value="create" />
<property name="javax.persistence.jdbc.user"
value="user" />
<property name="javax.persistence.jdbc.password"
value="password"/>
</properties>
</persistence-unit>
</persistence>
For
OpenJPA:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="TheWildZooPU" transaction-type="RESOURCE_LOCAL">
<!-- additional Mapping file, in addition to orm.xml>
<mapping-file>META-INF/JPAorm.xml</mapping-file>
<class>com.company.bean.jpa.PersistebleObjectImpl</class>
<class>com.company.bean.jpa.Animal</class>
<class>com.company.bean.jpa.Dog</class>
<class>com.company.bean.jpa.Cat</class>
<properties>
<property name="openjpa.ConnectionDriverName"
value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="openjpa.ConnectionURL"
value="jdbc:derby:target/database/jpa-test-database;create=true" />
<property name="openjpa.Log"
value="DefaultLevel=INFO,SQL=TRACE,File=./dist/jpaEnhancerLog.log,Runtime=INFO ,Tool=INFO" />
<property name="openjpa.ConnectionFactoryProperties"
value="PrettyPrint=true,PrettyPrintLineLength=72" />
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)" />
<property name="openjpa.ConnectionUserName"
value="user" />
<property name="openjpa.ConnectionPassword"
value="password"/>
</properties>
</persistence-unit>
</persistence>