Frequently Asked Questions about the OpenJPA project |
OpenJPA is a 100% open-source implementation of the Java Persistence API (JPA), which is the persistence component for EJB in the Java EE 5 specification.
OpenJPA has its roots in the popular Kodo product, which was created by SolarMetric, Inc. in 2001. BEA Systems, Inc. purchased SolarMetric in November of 2005, and soon thereafter announced that they would be donating the bulk of the code to the Apache Software Foundation. OpenJPA is the result of that donation.
Version 4.1 of Kodo will be based on the OpenJPA code base.
OpenJPA is a top-level project at the Apache Software Foundation.
Look at the Downloads page.
See Integration.
Check out the Get Involved page.
You can get version number and other details of OpenJPA jar you are using by:
java -jar /path/to/your/openjpa.jar |
OpenJPA provides configurable channel-based logging, as described in the chapter on Logging. The simplest example of enabling verbose logging is by using the following property in your persistence.xml file:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="1.0">
<persistence-unit name="example-logging" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="openjpa.Log" value="SQL=TRACE"/>
</properties>
</persistence-unit>
</persistence>
|
As of the 2.1.0 release, OpenJPA includes the Apache DBCP connection pool. You can also use any third-party connection pool that is configurable via the JDBC DataSource API (which most are). The following persistence.xml example shows how to use OpenJPA with a Apache Derby database and the Apache DBCP connection pool:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="1.0">
<persistence-unit name="example-derby" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="openjpa.ConnectionProperties"
value="DriverClassName=org.apache.derby.jdbc.ClientDriver,
Url=jdbc:derby://localhost:1527/database,
MaxActive=100,
MaxWait=10000,
TestOnBorrow=true,
Username=user,
Password=secret"/>
<property name="openjpa.ConnectionDriverName"
value="org.apache.commons.dbcp.BasicDataSource"/>
</properties>
</persistence-unit>
</persistence>
|
See the documentation on Using a Third-Party DataSource for further details.
Yes. OpenJPA can reorder and/or batch the SQL statements using different configurable strategies. The default strategy is capable of reordering the SQL statements to satisfy foreign key constraints. However ,you must tell OpenJPA to read the existing foreign key information from the database schema:
<property name="openjpa.jdbc.SchemaFactory" value="native(ForeignKeys=true)"/> |
See the documentation on Schema Factory for further details.
By default, OpenJPA does not create foreign key constraints on new tables that gets created according to O-R mapping annotation/descriptors. You can change this default behavior via following configuration property
<property name="openjpa.jdbc.MappingDefaults" value="ForeignKeyDeleteAction=restrict, JoinForeignKeyDeleteAction=restrict"/> |
to create foreign key constraints on the database tables generated by OpenJPA.
Yes. Standard JPA specification use a cross table to map one-sided one-to-many relation without a mappedBy inverse side. Often, you would like to create a one-to-many association based on an inverse foreign key (logical or actual) in the table of the related type. OpenJPA supports this mapping via @ElementJoinColumn annotation. The following example will map the collection of LineItem of a Subscription via a foreign key of LINEITEM table referring to primary key of SUBSCRIPTION table.
package org.mag.subscribe;
import org.apache.openjpa.persistence.jdbc.*;
@Entity
public class LineItem {
// has no inverse relation to Subscription
}
@Entity
@Table(name="SUB", schema="CNTRCT")
public class Subscription {
@Id
private long id;
@OneToMany
@ElementJoinColumn(name="SUB_ID", referencedColumnName="ID")
private Collection<LineItem> items;
...
}
|