<?xml version="1.0" encoding="UTF-8"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
-->
<chapter id="jpa_overview_arch">
    <title>
        Java Persistence API Architecture
    </title>
    <indexterm zone="jpa_overview_arch">
        <primary>
            JPA
        </primary>
        <secondary>
            architecture
        </secondary>
    </indexterm>
    <para>
The diagram below illustrates the relationships between the primary components
of the JPA architecture.
    </para>
    <mediaobject>
        <imageobject>
            <!-- PNG image data 400 x 256 (see README) -->
            <imagedata fileref="img/jpa-arch.png" width="267px"/>

        </imageobject>
        <textobject>
            <phrase>
                JPA architecture
            </phrase>
        </textobject>
    </mediaobject>
    <note>
        <para>
A number of the depicted interfaces are only required outside of an
EJB3-compliant application server. In an application server, <classname>
EntityManager</classname> instances are typically injected, rendering the
<classname>EntityManagerFactory</classname> unnecessary. Also, transactions
within an application server are handled using standard application server
transaction controls. Thus, the <classname>EntityTransaction</classname> also
goes unused.
        </para>
    </note>
    <itemizedlist>
        <listitem>
            <para>
            <indexterm>
                <primary>
                    Persistence
                </primary>
            </indexterm>
<emphasis role="bold"><link linkend="jpa_overview_persistence"><classname>
Persistence</classname></link></emphasis>: The <classname>
jakarta.persistence.Persistence</classname> class contains static helper methods
to obtain <classname>EntityManagerFactory</classname> instances in a
vendor-neutral fashion.
            </para>
        </listitem>
        <listitem>
            <para>
            <indexterm>
                <primary>
                    EntityManagerFactory
                </primary>
            </indexterm>
<emphasis role="bold"><link linkend="jpa_overview_emfactory"><classname>
EntityManagerFactory</classname></link></emphasis>: The <classname>
jakarta.persistence.EntityManagerFactory</classname> class is a factory for
<classname> EntityManager</classname> s.
            </para>
        </listitem>
        <listitem>
            <para>
            <indexterm>
                <primary>
                    EntityManager
                </primary>
            </indexterm>
<emphasis role="bold"><link linkend="jpa_overview_em"><classname>EntityManager
</classname></link></emphasis>: The <classname>jakarta.persistence.EntityManager
</classname> is the primary JPA interface used by applications. Each
<classname>EntityManager</classname> 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 <classname>
EntityManager</classname> and an <classname> EntityTransaction</classname>.
<classname> EntityManager</classname>s also act as factories for <classname>
Query</classname> instances.
            </para>
        </listitem>
        <listitem>
            <para>
            <indexterm>
                <primary>
                    entity
                </primary>
            </indexterm>
<emphasis role="bold"><link linkend="jpa_overview_pc"><classname>Entity
</classname></link></emphasis>: Entities are persistent objects that represent
datastore records.
            </para>
        </listitem>
        <listitem>
            <para>
            <indexterm>
                <primary>
                    EntityTransaction
                </primary>
            </indexterm>
<emphasis role="bold"><link linkend="jpa_overview_trans"><classname>
EntityTransaction</classname></link></emphasis>: Each <classname>EntityManager
</classname> has a one-to-one relation with a single <classname>
jakarta.persistence.EntityTransaction</classname>. <classname>
EntityTransaction</classname>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.
            </para>
        </listitem>
        <listitem>
            <para>
            <indexterm>
                <primary>
                    Query
                </primary>
            </indexterm>
            <indexterm>
                <primary>
                    Java Persistence Query Language
                </primary>
                <see>
                    JPQL
                </see>
            </indexterm>
            <indexterm>
                <primary>
                    JPQL
                </primary>
            </indexterm>
            <indexterm>
                <primary>
                    JPA
                </primary>
                <secondary>
                    query language
                </secondary>
                <see>
                    JPQL
                </see>
            </indexterm>
            <indexterm>
                <primary>
                    Structured Query Language
                </primary>
                <see>
                    SQL
                </see>
            </indexterm>
            <indexterm>
                <primary>
                    SQL
                </primary>
            </indexterm>
<emphasis role="bold"><link linkend="jpa_overview_query"><classname>Query
</classname></link></emphasis>: The <classname>jakarta.persistence.Query
</classname> 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 <classname>Query</classname> instances from an
<classname>EntityManager</classname>.
            </para>
        </listitem>
    </itemizedlist>
    <para>
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.
    </para>
    <example id="jpa_overview_arch_interact_outside">
        <title>
            Interaction of Interfaces Outside Container
        </title>
<programlisting>
  // get an EntityManagerFactory using the Persistence class
  // It is not recommended to obtain a factory often, as construction of a
  // factory is a costly operation. Typically you will like to cache
  // a factory and then refer it for repeated use
  EntityManagerFactory factory = Persistence.createEntityManagerFactory(null);

  // get an EntityManager from the factory
  EntityManager em = factory.createEntityManager();

  // Begin a transaction
  em.getTransaction().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 &gt; 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 will detect all updated entities and save them in database
  em.getTransaction().commit();

  // free the resources
  em.close();
</programlisting>
    </example>
    <para>
Within a container, the <classname>EntityManager</classname> will be injected
and transactions will be handled declaratively. Thus, the in-container version
of the example consists entirely of business logic:
    </para>
    <example id="jpa_overview_arch_interact_inside">
        <title>
            Interaction of Interfaces Inside Container
        </title>
<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 &gt; 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);
}
</programlisting>
    </example>
    <para>
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.
    </para>
    <section id="jpa_overview_arch_exceptions">
        <title>
            JPA Exceptions
        </title>
        <indexterm zone="jpa_overview_arch_exceptions">
            <primary>
                JPA
            </primary>
            <secondary>
                exceptions
            </secondary>
            <seealso>
                exceptions
            </seealso>
        </indexterm>
        <indexterm>
            <primary>
                exceptions
            </primary>
            <secondary>
                JPA
            </secondary>
        </indexterm>
        <mediaobject>
            <imageobject>
                <!-- PNG image data, 427 x 355 (see README) -->
                <imagedata fileref="img/jpa-exceptions.png" width="285px"/>

            </imageobject>
            <textobject>
                <phrase>
                    JPA exception architecture
                </phrase>
            </textobject>
        </mediaobject>
        <para>
The diagram above depicts the JPA exception architecture. All
exceptions are unchecked. JPA uses standard exceptions where
appropriate, most notably <classname>IllegalArgumentException</classname>s and
<classname>IllegalStateException</classname>s. The specification also provides
a few JPA-specific exceptions in the <literal>jakarta.persistence</literal>
package. These exceptions should be self-explanatory. See the
<ulink url="http://download.oracle.com/javaee/6/api/">Javadoc</ulink> for
additional details on JPA exceptions.
        </para>
        <note>
            <para>
All exceptions thrown by OpenJPA implement
<ulink url="../../apidocs/org/apache/openjpa/util/ExceptionInfo.html"><classname>
org.apache.openjpa.util.ExceptionInfo</classname></ulink> to provide you with
additional error information.
            </para>
        </note>
    </section>
</chapter>
