blob: 6286cdf24f35ec26576c7f7fade67bb5ec723358 [file] [log] [blame]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>4.&nbsp; Entity Identity Management</title><link rel="stylesheet" href="css/docbook.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.72.0"><link rel="start" href="manual.html" title="Apache OpenJPA User's Guide"><link rel="up" href="jpa_overview_em.html" title="Chapter&nbsp;8.&nbsp; EntityManager"><link rel="prev" href="jpa_overview_em_lifeexamples.html" title="3.&nbsp; Lifecycle Examples"><link rel="next" href="jpa_overview_em_cache.html" title="5.&nbsp; Cache Management"></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">4.&nbsp;
Entity Identity Management
</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="jpa_overview_em_lifeexamples.html">Prev</a>&nbsp;</td><th width="60%" align="center">Chapter&nbsp;8.&nbsp;
EntityManager
</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="jpa_overview_em_cache.html">Next</a></td></tr></table><hr></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="jpa_overview_em_identity"></a>4.&nbsp;
Entity Identity Management
</h2></div></div></div><p>
Each <code class="classname">EntityManager</code> is responsible for managing the
persistent identities of the managed objects in the persistence context. The
following methods allow you to interact with the management of persistent
identities. The behavior of these methods is deeply affected by the persistence
context type of the <code class="classname">EntityManager</code>; see
<a href="jpa_overview_emfactory_perscontext.html" title="3.&nbsp; Persistence Context">Section&nbsp;3, &#8220;
Persistence Context
&#8221;</a> for an explanation of
persistence contexts.
</p><pre class="programlisting">
public &lt;T&gt; T find(Class&lt;T&gt; cls, Object oid);
</pre><p>
<a class="indexterm" name="d0e5054"></a>
<a class="indexterm" name="d0e5062"></a>
<a class="indexterm" name="d0e5068"></a>
This method returns the persistent instance of the given type with the given
persistent identity. If the instance is already present in the current
persistence context, the cached version will be returned. Otherwise, a new
instance will be constructed and loaded with state from the datastore. If no
entity with the given type and identity exists in the datastore, this method
returns null.
</p><pre class="programlisting">
public &lt;T&gt; T getReference(Class&lt;T&gt; cls, Object oid);
</pre><p>
<a class="indexterm" name="d0e5078"></a>
<a class="indexterm" name="d0e5086"></a>
<a class="indexterm" name="d0e5092"></a>
<a class="indexterm" name="d0e5098"></a>
This method is similar to <code class="methodname">find</code>, but does not
necessarily go to the database when the entity is not found in cache. The
implementation may construct a <span class="emphasis"><em>hollow</em></span> entity and return it
to you instead. Hollow entities do not have any state loaded. The state only
gets loaded when you attempt to access a persistent field. At that time, the
implementation may throw an <code class="classname">EntityNotFoundException</code> if it
discovers that the entity does not exist in the datastore. The implementation
may also throw an <code class="classname">EntityNotFoundException</code> from the
<code class="methodname">getReference</code> method itself. Unlike <code class="methodname">
find</code>, <code class="methodname">getReference</code> does not return null.
</p><pre class="programlisting">
public boolean contains(Object entity);
</pre><p>
<a class="indexterm" name="d0e5127"></a>
<a class="indexterm" name="d0e5133"></a>
Returns true if the given entity is part of the current persistence context, and
false otherwise. Removed entities are not considered part of the current
persistence context.
</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="jpa_overview_em_lifeexamples.html">Prev</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="jpa_overview_em.html">Up</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="jpa_overview_em_cache.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">3.&nbsp;
Lifecycle Examples
&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;5.&nbsp;
Cache Management
</td></tr></table></div></body></html>