| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>4. Entity Identity Management</title><base href="display"><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="manual.html" title="Apache OpenJPA 2.4 User's Guide"><link rel="up" href="jpa_overview_em.html" title="Chapter 8. EntityManager"><link rel="prev" href="jpa_overview_em_lifeexamples.html" title="3. Lifecycle Examples"><link rel="next" href="jpa_overview_em_cache.html" title="5. 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. |
| Entity Identity Management |
| </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="jpa_overview_em_lifeexamples.html">Prev</a> </td><th width="60%" align="center">Chapter 8. |
| EntityManager |
| </th><td width="20%" align="right"> <a accesskey="n" href="jpa_overview_em_cache.html">Next</a></td></tr></table><hr></div><div class="section" id="jpa_overview_em_identity"><div class="titlepage"><div><div><h2 class="title" style="clear: both">4. |
| 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 class="xref" href="jpa_overview_emfactory_perscontext.html" title="3. Persistence Context">Section 3, “ |
| Persistence Context |
| ”</a> for an explanation of |
| persistence contexts. |
| </p> |
| <pre class="programlisting"> |
| public <T> T find(Class<T> cls, Object oid); |
| </pre> |
| <p> |
| <a class="indexterm" name="d5e2574"></a> |
| <a class="indexterm" name="d5e2578"></a> |
| <a class="indexterm" name="d5e2581"></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 <T> T getReference(Class<T> cls, Object oid); |
| </pre> |
| <p> |
| <a class="indexterm" name="d5e2586"></a> |
| <a class="indexterm" name="d5e2590"></a> |
| <a class="indexterm" name="d5e2593"></a> |
| <a class="indexterm" name="d5e2596"></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="d5e2607"></a> |
| <a class="indexterm" name="d5e2610"></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> </td><td width="20%" align="center"><a accesskey="u" href="jpa_overview_em.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="jpa_overview_em_cache.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">3. |
| Lifecycle Examples |
| </td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top"> 5. |
| Cache Management |
| </td></tr></table></div></body></html> |