blob: d00adebcee4138cf02f5d6b41997cdef4c9cefe6 [file] [log] [blame]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>2.&nbsp; JPA Extensions</title><base href="display"><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><link rel="home" href="manual.html" title="Apache OpenJPA 2.4 User's Guide"><link rel="up" href="ref_guide_runtime.html" title="Chapter&nbsp;9.&nbsp; Runtime Extensions"><link rel="prev" href="ref_guide_runtime.html" title="Chapter&nbsp;9.&nbsp; Runtime Extensions"><link rel="next" href="ref_guide_locking.html" title="3.&nbsp; Object Locking"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">2.&nbsp;
JPA Extensions
</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ref_guide_runtime.html">Prev</a>&nbsp;</td><th width="60%" align="center">Chapter&nbsp;9.&nbsp;
Runtime Extensions
</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="ref_guide_locking.html">Next</a></td></tr></table><hr></div><div class="section" id="ref_guide_runtime_jpa"><div class="titlepage"><div><div><h2 class="title" style="clear: both">2.&nbsp;
JPA Extensions
</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="section"><a href="ref_guide_runtime_jpa.html#ref_guide_runtime_emfactory">2.1.
OpenJPAEntityManagerFactory
</a></span></dt><dt><span class="section"><a href="ref_guide_runtime_jpa.html#ref_guide_runtime_em">2.2.
OpenJPAEntityManager
</a></span></dt><dt><span class="section"><a href="ref_guide_runtime_jpa.html#ref_guide_runtime_jpaquery">2.3.
OpenJPAQuery
</a></span></dt><dt><span class="section"><a href="ref_guide_runtime_jpa.html#ref_guide_runtime_jpaextent">2.4.
Extent
</a></span></dt><dt><span class="section"><a href="ref_guide_runtime_jpa.html#ref_guide_runtime_jpacache">2.5.
StoreCache
</a></span></dt><dt><span class="section"><a href="ref_guide_runtime_jpa.html#ref_guide_runtime_jpaquerycache">2.6.
QueryResultCache
</a></span></dt><dt><span class="section"><a href="ref_guide_runtime_jpa.html#ref_guide_runtime_jpafetch">2.7.
FetchPlan
</a></span></dt><dt><span class="section"><a href="ref_guide_runtime_jpa.html#ref_guide_runtime_openjpaentitytransaction">2.8.
OpenJPAEntityTransaction
</a></span></dt><dt><span class="section"><a href="ref_guide_runtime_jpa.html#ref_guide_runtime_openjpapersistence">2.9.
OpenJPAPersistence
</a></span></dt></dl></div>
<p>
The following sections outline the runtime interfaces you can use to access
OpenJPA-specific functionality from JPA. Each interface contains services and
convenience methods missing from the JPA specification. OpenJPA strives to use
the same naming conventions and API patterns as standard JPA methods in all
extensions, so that OpenJPA extension APIs feel as much as possible like
standard JPA.
</p>
<p>
You may have noticed the examples throughout this document using the
<code class="methodname">OpenJPAPersistence.cast</code> methods to cast from standard
JPA interfaces to OpenJPA extended interfaces. This is the recommended practice.
Some application server vendors may proxy OpenJPA's JPA implementation,
preventing a straight cast. <code class="classname">OpenJPAPersistence</code>'s
<code class="methodname">cast</code> methods work around these proxies.
</p>
<pre class="programlisting">
public static OpenJPAEntityManagerFactory cast(EntityManagerFactory emf);
public static OpenJPAEntityManager cast(EntityManager em);
public static OpenJPAQuery cast(Query q);
</pre>
<p>
We provide additional information on the <code class="classname">OpenJPAPersistence
</code> helper <a class="link" href="ref_guide_runtime_jpa.html#ref_guide_runtime_openjpapersistence" title="2.9.&nbsp; OpenJPAPersistence">
below</a>.
</p>
<div class="section" id="ref_guide_runtime_emfactory"><div class="titlepage"><div><div><h3 class="title">2.1.&nbsp;
OpenJPAEntityManagerFactory
</h3></div></div></div>
<a class="indexterm" name="d5e15424"></a>
<a class="indexterm" name="d5e15426"></a>
<p>
The <code class="classname">org.apache.openjpa.persistence.OpenJPAEntityManagerFactory
</code> interface extends the basic <code class="classname">
javax.persistence.EntityManagerFactory</code> with OpenJPA-specific
features. The <code class="classname">OpenJPAEntityManagerFactory</code> offers APIs to
access the OpenJPA data and query caches and to perform other OpenJPA-specific
operations. See the
<a class="ulink" href="../../apidocs/org/apache/openjpa/persistence/OpenJPAEntityManagerFactory.html" target="_top">
interface Javadoc</a> for details.
</p>
</div>
<div class="section" id="ref_guide_runtime_em"><div class="titlepage"><div><div><h3 class="title">2.2.&nbsp;
OpenJPAEntityManager
</h3></div></div></div>
<a class="indexterm" name="d5e15437"></a>
<a class="indexterm" name="d5e15439"></a>
<p>
All OpenJPA <code class="classname">EntityManager</code>s implement the
<a class="ulink" href="../../apidocs/org/apache/openjpa/persistence/OpenJPAEntityManager.html" target="_top">
<code class="classname">org.apache.openjpa.persistence.OpenJPAEntityManager</code>
</a> interface. This interface extends the standard <code class="classname">
javax.persistence.EntityManager</code>. Just as the standard <code class="classname">
EntityManager</code> is the primary window into JPA services, the
<code class="classname">OpenJPAEntityManager</code> is the primary window from JPA into
OpenJPA-specific functionality. We strongly encourage you to investigate the API
extensions this interface contains.
</p>
</div>
<div class="section" id="ref_guide_runtime_jpaquery"><div class="titlepage"><div><div><h3 class="title">2.3.&nbsp;
OpenJPAQuery
</h3></div></div></div>
<a class="indexterm" name="d5e15452"></a>
<a class="indexterm" name="d5e15454"></a>
<p>
OpenJPA extends JPA's standard query functionality with the <code class="classname">
org.apache.openjpa.persistence.OpenJPAQuery</code> interface. See its
<a class="ulink" href="../../apidocs/org/apache/openjpa/persistence/OpenJPAQuery.html" target="_top">Javadoc
</a> for details on the convenience methods it provides.
</p>
</div>
<div class="section" id="ref_guide_runtime_jpaextent"><div class="titlepage"><div><div><h3 class="title">2.4.&nbsp;
Extent
</h3></div></div></div>
<a class="indexterm" name="d5e15463"></a>
<a class="indexterm" name="d5e15465"></a>
<p>
An <code class="classname">Extent</code> is a logical view of all persistent instances
of a given entity class, possibly including subclasses. OpenJPA adds the
<a class="ulink" href="../../apidocs/org/apache/openjpa/persistence/Extent.html" target="_top"><code class="classname">
org.apache.openjpa.persistence.Extent</code></a> class to the set of
Java Persistence APIs. The following code illustrates iterating over all
instances of the <code class="classname">Magazine</code> entity, without subclasses:
</p>
<div class="example" id="ref_guide_runtime_jpaextentex"><p class="title"><b>Example&nbsp;9.2.&nbsp;
Using a JPA Extent
</b></p><div class="example-contents">
<pre class="programlisting">
import org.apache.openjpa.persistence.*;
...
OpenJPAEntityManager kem = OpenJPAPersistence.cast(em);
Extent&lt;Magazine&gt; mags = kem.createExtent(Magazine.class, false);
for (Magazine m : mags)
processMagazine(m);
</pre>
</div></div><br class="example-break">
</div>
<div class="section" id="ref_guide_runtime_jpacache"><div class="titlepage"><div><div><h3 class="title">2.5.&nbsp;
StoreCache
</h3></div></div></div>
<a class="indexterm" name="d5e15479"></a>
<p>
In addition to the <code class="classname">EntityManager</code> object cache the JPA
specification provides access to a second level cache via the
javax.persistence.Cache interface. OpenJPA provides further extensions via
the org.apache.openjpa.persistence.StoreCache interface documented at
<a class="ulink" href="../../apidocs/org/apache/openjpa/persistence/StoreCache.html" target="_top">
<code class="classname">org.apache.openjpa.persistence.StoreCache</code></a>.
<a class="xref" href="ref_guide_caching.html#ref_guide_cache" title="1.&nbsp; Data Cache">Section&nbsp;1, &#8220;
Data Cache
&#8221;</a> has detailed information on OpenJPA's
data caching system, including the <code class="classname">StoreCache</code> facade.
</p>
</div>
<div class="section" id="ref_guide_runtime_jpaquerycache"><div class="titlepage"><div><div><h3 class="title">2.6.&nbsp;
QueryResultCache
</h3></div></div></div>
<a class="indexterm" name="d5e15489"></a>
<p>
OpenJPA can cache query results as well as persistent object data. The
<a class="ulink" href="../../apidocs/org/apache/openjpa/persistence/QueryResultCache.html" target="_top">
<code class="classname">org.apache.openjpa.persistence.QueryResultCache</code></a>
is an JPA-flavored facade to OpenJPA's internal query cache. See
<a class="xref" href="ref_guide_caching.html#ref_guide_cache_query" title="1.4.&nbsp; Query Cache">Section&nbsp;1.4, &#8220;
Query Cache
&#8221;</a> for details on query caching in
OpenJPA.
</p>
</div>
<div class="section" id="ref_guide_runtime_jpafetch"><div class="titlepage"><div><div><h3 class="title">2.7.&nbsp;
FetchPlan
</h3></div></div></div>
<a class="indexterm" name="d5e15497"></a>
<a class="indexterm" name="d5e15499"></a>
<p>
Many of the aforementioned OpenJPA interfaces give you access to an <code class="classname">
org.apache.openjpa.persistence.FetchPlan</code> instance. The <code class="classname">
FetchPlan</code> allows you to exercise some control over how objects are
fetched from the datastore, including <a class="link" href="ref_guide_dbsetup_lrs.html" title="10.&nbsp; Large Result Sets">
large result set support</a>, <a class="link" href="ref_guide_fetch.html" title="7.&nbsp; Fetch Groups">custom fetch
groups</a>, and <a class="link" href="ref_guide_locking.html" title="3.&nbsp; Object Locking">lock levels</a>.
</p>
<p>
OpenJPA goes one step further, extending <code class="classname">FetchPlan</code> with
<a class="ulink" href="../../apidocs/org/apache/openjpa/persistence/jdbc/JDBCFetchPlan.html" target="_top">
<code class="classname">org.apache.openjpa.persistence.jdbc.JDBCFetchPlan</code></a>
to add additional JDBC-specific tuning methods. Unless you have customized
OpenJPA to use a non-relational back-end (see
<a class="xref" href="ref_guide_enterprise_abstractstore.html" title="8.&nbsp; Non-Relational Stores">Section&nbsp;8, &#8220;
Non-Relational Stores
&#8221;</a> ), all <code class="classname">
FetchPlan</code>s in OpenJPA implement <code class="classname">
JDBCFetchPlan</code>, so feel free to cast to this interface.
</p>
<p>
Fetch plans pass on from parent components to child components. The <code class="classname">
EntityManagerFactory</code> settings (via your configuration properties)
for things like the fetch size, result set type, and custom fetch groups are
passed on to the fetch plan of the <code class="classname">EntityManager</code>s it
produces. The settings of each <code class="classname">EntityManager</code>, in turn,
are passed on to each <code class="classname">Query</code> and <code class="classname">Extent
</code> it returns. Note that the opposite, however, is not true. Modifying
the fetch plan of a <code class="classname">Query</code> or <code class="classname">Extent
</code> does not affect the <code class="classname">EntityManager</code>'s
configuration. Likewise, modifying an <code class="classname">EntityManager</code>'s
configuration does not affect the <code class="classname"> EntityManagerFactory</code>.
</p>
<p>
<a class="xref" href="ref_guide_fetch.html" title="7.&nbsp; Fetch Groups">Section&nbsp;7, &#8220;
Fetch Groups
&#8221;</a> includes examples using <code class="classname">
FetchPlan</code>s.
</p>
</div>
<div class="section" id="ref_guide_runtime_openjpaentitytransaction"><div class="titlepage"><div><div><h3 class="title">2.8.&nbsp;
OpenJPAEntityTransaction
</h3></div></div></div>
<a class="indexterm" name="d5e15531"></a>
<p>
<a class="ulink" href="../../apidocs/org/apache/openjpa/persistence/OpenJPAEntityTransaction.html" target="_top">
<code class="classname">org.apache.openjpa.persistence.OpenJPAEntityTransaction</code></a>
extends <code class="classname">javax.persistence.EntityTransaction</code> to provide
additional transaction-debugging capabilities and some concurrency-related
commit and rollback features.
</p>
</div>
<div class="section" id="ref_guide_runtime_openjpapersistence"><div class="titlepage"><div><div><h3 class="title">2.9.&nbsp;
OpenJPAPersistence
</h3></div></div></div>
<a class="indexterm" name="d5e15539"></a>
<p>
<a class="ulink" href="../../apidocs/org/apache/openjpa/persistence/OpenJPAPersistence.html" target="_top">
<code class="classname">org.apache.openjpa.persistence.OpenJPAPersistence</code></a>
is a static helper class that adds OpenJPA-specific utility methods to
<code class="classname">javax.persistence.Persistence</code>.
</p>
</div>
</div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ref_guide_runtime.html">Prev</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="ref_guide_runtime.html">Up</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="ref_guide_locking.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter&nbsp;9.&nbsp;
Runtime Extensions
&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;3.&nbsp;
Object Locking
</td></tr></table></div></body></html>