| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>Chapter 3. Java Persistence API Architecture</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.html" title="Part 2. Java Persistence API"><link rel="prev" href="jpa_overview_why.html" title="Chapter 2. Why JPA?"><link rel="next" href="jpa_overview_pc.html" title="Chapter 4. Entity"></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">Chapter 3. |
| Java Persistence API Architecture |
| </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="jpa_overview_why.html">Prev</a> </td><th width="60%" align="center">Part 2. Java Persistence API</th><td width="20%" align="right"> <a accesskey="n" href="jpa_overview_pc.html">Next</a></td></tr></table><hr></div><div class="chapter" lang="en" id="jpa_overview_arch"><div class="titlepage"><div><div><h2 class="title"><a name="jpa_overview_arch"></a>Chapter 3. |
| Java Persistence API Architecture |
| </h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="jpa_overview_arch.html#jpa_overview_arch_exceptions">1. |
| JPA Exceptions |
| </a></span></dt></dl></div><a class="indexterm" name="d0e594"></a><p> |
| The diagram below illustrates the relationships between the primary components |
| of the JPA architecture. |
| </p><div class="mediaobject"><table border="0" summary="manufactured viewport for HTML img" cellspacing="0" cellpadding="0" width="267"><tr><td><img src="img/jpa-arch.png"></td></tr></table></div><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p> |
| A number of the depicted interfaces are only required outside of an |
| EJB3-compliant application server. In an application server, <code class="classname"> |
| EntityManager</code> instances are typically injected, rendering the |
| <code class="classname">EntityManagerFactory</code> unnecessary. Also, transactions |
| within an application server are handled using standard application server |
| transaction controls. Thus, the <code class="classname">EntityTransaction</code> also |
| goes unused. |
| </p></div><div class="itemizedlist"><ul type="disc"><li><p> |
| <a class="indexterm" name="d0e624"></a> |
| <span class="bold"><strong><a href="jpa_overview_persistence.html" title="Chapter 6. Persistence"><code class="classname"> |
| Persistence</code></a></strong></span>: The <code class="classname"> |
| javax.persistence.Persistence</code> class contains static helper methods |
| to obtain <code class="classname">EntityManagerFactory</code> instances in a |
| vendor-neutral fashion. |
| </p></li><li><p> |
| <a class="indexterm" name="d0e642"></a> |
| <span class="bold"><strong><a href="jpa_overview_emfactory.html" title="Chapter 7. EntityManagerFactory"><code class="classname"> |
| EntityManagerFactory</code></a></strong></span>: The <code class="classname"> |
| javax.persistence.EntityManagerFactory</code> class is a factory for |
| <code class="classname"> EntityManager</code> s. |
| </p></li><li><p> |
| <a class="indexterm" name="d0e660"></a> |
| <span class="bold"><strong><a href="jpa_overview_em.html" title="Chapter 8. EntityManager"><code class="classname">EntityManager |
| </code></a></strong></span>: The <code class="classname">javax.persistence.EntityManager |
| </code> is the primary JPA interface used by applications. Each |
| <code class="classname">EntityManager</code> manages a set of persistent objects, and |
| has APIs to insert new objects and delete existing ones. When used outside the |
| container, there is a one-to-one relationship between an <code class="classname"> |
| EntityManager</code> and an <code class="classname"> EntityTransaction</code>. |
| <code class="classname"> EntityManager</code>s also act as factories for <code class="classname"> |
| Query</code> instances. |
| </p></li><li><p> |
| <a class="indexterm" name="d0e690"></a> |
| <span class="bold"><strong><a href="jpa_overview_pc.html" title="Chapter 4. Entity"><code class="classname">Entity |
| </code></a></strong></span>: Entites are persistent objects that represent |
| datastore records. |
| </p></li><li><p> |
| <a class="indexterm" name="d0e702"></a> |
| <span class="bold"><strong><a href="jpa_overview_trans.html" title="Chapter 9. Transaction"><code class="classname"> |
| EntityTransaction</code></a></strong></span>: Each <code class="classname">EntityManager |
| </code> has a one-to-one relation with a single <code class="classname"> |
| javax.persistence.EntityTransaction</code>. <code class="classname"> |
| EntityTransaction</code>s allow operations on persistent data to be |
| grouped into units of work that either completely succeed or completely fail, |
| leaving the datastore in its original state. These all-or-nothing operations |
| are important for maintaining data integrity. |
| </p></li><li><p> |
| <a class="indexterm" name="d0e723"></a> |
| <a class="indexterm" name="d0e727"></a> |
| <a class="indexterm" name="d0e733"></a> |
| <a class="indexterm" name="d0e737"></a> |
| <a class="indexterm" name="d0e745"></a> |
| <a class="indexterm" name="d0e751"></a> |
| <span class="bold"><strong><a href="jpa_overview_query.html" title="Chapter 10. JPA Query"><code class="classname">Query |
| </code></a></strong></span>: The <code class="classname">javax.persistence.Query |
| </code> interface is implemented by each JPA vendor to find persistent |
| objects that meet certain criteria. JPA standardizes support for queries using |
| both the Java Persistence Query Language (JPQL) and the Structured Query |
| Language (SQL). You obtain <code class="classname">Query</code> instances from an |
| <code class="classname">EntityManager</code>. |
| </p></li></ul></div><p> |
| The example below illustrates how the JPA interfaces interact to execute a JPQL |
| query and update persistent objects. The example assumes execution outside a |
| container. |
| </p><div class="example"><a name="jpa_overview_arch_interact_outside"></a><p class="title"><b>Example 3.1. |
| Interaction of Interfaces Outside Container |
| </b></p><div class="example-contents"><pre class="programlisting"> |
| // get an EntityManagerFactory using the Persistence class; typically |
| // the factory is cached for easy repeated use |
| EntityManagerFactory factory = Persistence.createEntityManagerFactory(null); |
| |
| // get an EntityManager from the factory |
| EntityManager em = factory.createEntityManager(PersistenceContextType.EXTENDED); |
| |
| // updates take place within transactions |
| EntityTransaction tx = em.getTransaction(); |
| tx.begin(); |
| |
| // query for all employees who work in our research division |
| // and put in over 40 hours a week average |
| Query query = em.createQuery("select e from Employee e where " |
| + "e.division.name = 'Research' AND e.avgHours > 40"); |
| List results = query.getResultList (); |
| |
| // give all those hard-working employees a raise |
| for (Object res : results) { |
| Employee emp = (Employee) res; |
| emp.setSalary(emp.getSalary() * 1.1); |
| } |
| |
| // commit the updates and free resources |
| tx.commit(); |
| em.close(); |
| factory.close(); |
| </pre></div></div><br class="example-break"><p> |
| Within a container, the <code class="classname">EntityManager</code> will be injected |
| and transactions will be handled declaratively. Thus, the in-container version |
| of the example consists entirely of business logic: |
| </p><div class="example"><a name="jpa_overview_arch_interact_inside"></a><p class="title"><b>Example 3.2. |
| Interaction of Interfaces Inside Container |
| </b></p><div class="example-contents"><pre class="programlisting"> |
| // query for all employees who work in our research division |
| // and put in over 40 hours a week average - note that the EntityManager em |
| // is injected using a @Resource annotation |
| Query query = em.createQuery("select e from Employee e where " |
| + "e.division.name = 'Research' and e.avgHours > 40"); |
| List results = query.getResultList(); |
| |
| // give all those hard-working employees a raise |
| for (Object res : results) { |
| emp = (Employee) res; |
| emp.setSalary(emp.getSalary() * 1.1); |
| } |
| </pre></div></div><br class="example-break"><p> |
| The remainder of this document explores the JPA interfaces in detail. We present |
| them in roughly the order that you will use them as you develop your |
| application. |
| </p><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="jpa_overview_arch_exceptions"></a>1. |
| JPA Exceptions |
| </h2></div></div></div><a class="indexterm" name="d0e791"></a><a class="indexterm" name="d0e798"></a><div class="mediaobject"><table border="0" summary="manufactured viewport for HTML img" cellspacing="0" cellpadding="0" width="285"><tr><td><img src="img/jpa-exceptions.png"></td></tr></table></div><p> |
| The diagram above depicts the JPA exception architecture. All |
| exceptions are unchecked. JPA uses standard exceptions where |
| appropriate, most notably <code class="classname">IllegalArgumentException</code>s and |
| <code class="classname">IllegalStateException</code>s. The specification also provides |
| a few JPA-specific exceptions in the <code class="literal">javax.persistence</code> |
| package. These exceptions should be self-explanatory. See the |
| <a xmlns:xlink="http://www.w3.org/1999/xlink" href="http://java.sun.com/javaee/5/docs/api" target="_top">Javadoc</a> for |
| additional details on JPA exceptions. |
| </p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p> |
| All exceptions thrown by OpenJPA implement |
| <a xmlns:xlink="http://www.w3.org/1999/xlink" href="../javadoc/org/apache/openjpa/util/ExceptionInfo.html" target="_top"><code class="classname"> |
| org.apache.openjpa.util.ExceptionInfo</code></a> to provide you with |
| additional error information. |
| </p></div></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="jpa_overview_why.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="jpa_overview.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="jpa_overview_pc.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 2. |
| Why JPA? |
| </td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 4. |
| Entity |
| </td></tr></table></div></body></html> |