|  | <html><head> | 
|  | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | 
|  | <title>3.  Lifecycle Examples</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 8.  EntityManager"><link rel="prev" href="jpa_overview_em_lifecycle.html" title="2.  Entity Lifecycle Management"><link rel="next" href="jpa_overview_em_identity.html" title="4.  Entity Identity 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">3.  | 
|  | Lifecycle Examples | 
|  | </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="jpa_overview_em_lifecycle.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_identity.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_lifeexamples"></a>3.  | 
|  | Lifecycle Examples | 
|  | </h2></div></div></div><p> | 
|  | The examples below demonstrate how to use the lifecycle methods presented in the | 
|  | previous section. The examples are appropriate for out-of-container use. Within | 
|  | a container, <code class="classname"> EntityManager</code>s are usually injected, and | 
|  | transactions are usually managed. You would therefore omit the <code class="methodname"> | 
|  | createEntityManager</code> and <code class="methodname">close</code> calls, as | 
|  | well as all transaction demarcation code. | 
|  | </p><div class="example"><a name="jpa_overview_em_lifecycle_persist"></a><p class="title"><b>Example 8.1.  | 
|  | Persisting Objects | 
|  | </b></p><div class="example-contents"><a class="indexterm" name="d0e4979"></a><pre class="programlisting"> | 
|  | // create some objects | 
|  | Magazine mag = new Magazine("1B78-YU9L", "JavaWorld"); | 
|  |  | 
|  | Company pub = new Company("Weston House"); | 
|  | pub.setRevenue(1750000D); | 
|  | mag.setPublisher(pub); | 
|  | pub.addMagazine(mag); | 
|  |  | 
|  | Article art = new Article("JPA Rules!", "Transparent Object Persistence"); | 
|  | art.addAuthor(new Author("Fred", "Hoyle")); | 
|  | mag.addArticle(art); | 
|  |  | 
|  | // persist | 
|  | EntityManager em = emf.createEntityManager(); | 
|  | em.getTransaction().begin(); | 
|  | em.persist(mag); | 
|  | em.persist(pub); | 
|  | em.persist(art); | 
|  | em.getTransaction().commit(); | 
|  |  | 
|  | // or we could continue using the EntityManager... | 
|  | em.close(); | 
|  | </pre></div></div><br class="example-break"><div class="example"><a name="jpa_overview_em_lifecycle_update"></a><p class="title"><b>Example 8.2.  | 
|  | Updating Objects | 
|  | </b></p><div class="example-contents"><a class="indexterm" name="d0e4991"></a><pre class="programlisting"> | 
|  | Magazine.MagazineId mi = new Magazine.MagazineId(); | 
|  | mi.isbn = "1B78-YU9L"; | 
|  | mi.title = "JavaWorld"; | 
|  |  | 
|  | // updates should always be made within transactions; note that | 
|  | // there is no code explicitly linking the magazine or company | 
|  | // with the transaction; JPA automatically tracks all changes | 
|  | EntityManager em = emf.createEntityManager(); | 
|  | em.getTransaction().begin(); | 
|  | Magazine mag = em.find(Magazine.class, mi); | 
|  | mag.setPrice(5.99); | 
|  | Company pub = mag.getPublisher(); | 
|  | pub.setRevenue(1750000D); | 
|  | em.getTransaction().commit(); | 
|  |  | 
|  | // or we could continue using the EntityManager... | 
|  | em.close(); | 
|  | </pre></div></div><br class="example-break"><div class="example"><a name="jpa_overview_em_lifecycle_delete"></a><p class="title"><b>Example 8.3.  | 
|  | Removing Objects | 
|  | </b></p><div class="example-contents"><a class="indexterm" name="d0e5003"></a><pre class="programlisting"> | 
|  | // assume we have an object id for the company whose subscriptions | 
|  | // we want to delete | 
|  | Object oid = ...; | 
|  |  | 
|  | // deletes should always be made within transactions | 
|  | EntityManager em = emf.createEntityManager(); | 
|  | em.getTransaction().begin(); | 
|  | Company pub = (Company) em.find(Company.class, oid); | 
|  | for (Subscription sub : pub.getSubscriptions()) | 
|  | em.remove(sub); | 
|  | pub.getSubscriptions().clear(); | 
|  | em.getTransaction().commit(); | 
|  |  | 
|  | // or we could continue using the EntityManager... | 
|  | em.close(); | 
|  | </pre></div></div><br class="example-break"><div class="example"><a name="jpa_overview_em_detachex"></a><p class="title"><b>Example 8.4.  | 
|  | Detaching and Merging | 
|  | </b></p><div class="example-contents"><p> | 
|  | This example demonstrates a common client/server scenario. The client requests | 
|  | objects and makes changes to them, while the server handles the object lookups | 
|  | and transactions. | 
|  | </p><pre class="programlisting"> | 
|  | // CLIENT: | 
|  | // requests an object with a given oid | 
|  | Record detached = (Record) getFromServer(oid); | 
|  |  | 
|  | ... | 
|  |  | 
|  | // SERVER: | 
|  | // send object to client; object detaches on EM close | 
|  | Object oid = processClientRequest(); | 
|  | EntityManager em = emf.createEntityManager(); | 
|  | Record record = em.find(Record.class, oid); | 
|  | em.close(); | 
|  | sendToClient(record); | 
|  |  | 
|  | ... | 
|  |  | 
|  | // CLIENT: | 
|  | // makes some modifications and sends back to server | 
|  | detached.setSomeField("bar"); | 
|  | sendToServer(detached); | 
|  |  | 
|  | ... | 
|  |  | 
|  | // SERVER: | 
|  | // merges the instance and commit the changes | 
|  | Record modified = (Record) processClientRequest(); | 
|  | EntityManager em = emf.createEntityManager(); | 
|  | em.getTransaction().begin(); | 
|  | Record merged = (Record) em.merge(modified); | 
|  | merged.setLastModified(System.currentTimeMillis()); | 
|  | merged.setModifier(getClientIdentityCode()); | 
|  | em.getTransaction().commit(); | 
|  | em.close(); | 
|  | </pre></div></div><br class="example-break"></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="jpa_overview_em_lifecycle.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_identity.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">2.  | 
|  | Entity Lifecycle Management | 
|  |  </td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top"> 4.  | 
|  | Entity Identity Management | 
|  | </td></tr></table></div></body></html> |