You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by mp...@apache.org on 2006/07/26 09:36:40 UTC
svn commit: r425652 [2/3] - in /incubator/openjpa/trunk: openjpa-project/
openjpa-project/src/doc/manual/ openjpa-project/src/site/
openjpa-project/src/site/resources/ openjpa-project/src/site/resources/css/
openjpa-project/src/site/resources/img/ src/...
Modified: incubator/openjpa/trunk/openjpa-project/src/doc/manual/manual.xml
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-project/src/doc/manual/manual.xml?rev=425652&r1=425651&r2=425652&view=diff
==============================================================================
--- incubator/openjpa/trunk/openjpa-project/src/doc/manual/manual.xml (original)
+++ incubator/openjpa/trunk/openjpa-project/src/doc/manual/manual.xml Wed Jul 26 00:36:39 2006
@@ -12,56 +12,55 @@
<primary>OpenJPA <phrase>JPA</phrase></primary>
</indexterm>
<para>
- OpenJPA <phrase>JPA</phrase> is Apache's implementation of Sun's
- <phrase>
- Java Persistence API (JPA) specification</phrase>
-
-
- for the transparent persistence of Java objects.
- This document provides an overview of
- <phrase>the JPA standard</phrase>
-
-
- and technical details on the use of OpenJPA <phrase>JPA</phrase>.
- </para>
+ OpenJPA is Apache's implementation of Sun's
+ <phrase>Java Persistence API (JPA) specification</phrase>
+
+
+ for the transparent persistence of Java objects.
+ This document provides an overview of
+ <phrase>the JPA standard</phrase>
+
+
+ and technical details on the use of OpenJPA <phrase>JPA</phrase>.
+ </para>
<para>
- To quickly get started with JPA, you may want to begin at
- <xref linkend="jpa_tutorial"/>.
- If you would prefer to start with an introduction to the concepts of JPA,
- begin with <xref linkend="jpa_overview_intro"/>.
- </para>
+ To quickly get started with JPA, you may want to begin at
+ <xref linkend="jpa_tutorial"/>.
+ If you would prefer to start with an introduction to the concepts of JPA,
+ begin with <xref linkend="jpa_overview_intro"/>.
+ </para>
<section id="openjpa_intro_about">
<title>About This Document</title>
<para>
- This document is intended for OpenJPA users. It is divided into several
- parts:
- </para>
+ This document is intended for OpenJPA users. It is divided into several
+ parts:
+ </para>
<itemizedlist>
<listitem>
<para>
- The <link linkend="jpa_overview_intro">JPA Overview</link>
- describes the fundamentals of JPA.
- </para>
+ The <link linkend="jpa_overview_intro">JPA Overview</link>
+ describes the fundamentals of JPA.
+ </para>
</listitem>
<listitem>
<para>
- In the <link linkend="tutorials">OpenJPA <phrase>JPA</phrase>
- Tutorials</link> you will develop simple persistent applications
- using OpenJPA. Through the tutorials' hands-on approach, you
- will become comfortable with the core tools and development
- processes under OpenJPA <phrase>JPA</phrase>.
- </para>
+ In the <link linkend="tutorials">OpenJPA <phrase>JPA</phrase>
+ Tutorials</link> you will develop simple persistent applications
+ using OpenJPA. Through the tutorials' hands-on approach, you
+ will become comfortable with the core tools and development
+ processes under OpenJPA <phrase>JPA</phrase>.
+ </para>
</listitem>
<listitem>
<para>
- The <link linkend="ref_guide_intro">OpenJPA <phrase>JPA</phrase>
- Reference Guide</link> contains detailed documentation on all
- aspects of OpenJPA <phrase>JPA</phrase>. Browse through this guide to
- familiarize yourself with the many advanced features and
- customization opportunities OpenJPA provides. Later, you can use
- the guide when you need details on a specific aspect of OpenJPA
- <phrase>JPA</phrase>.
- </para>
+ The <link linkend="ref_guide_intro">OpenJPA <phrase>JPA</phrase>
+ Reference Guide</link> contains detailed documentation on all
+ aspects of OpenJPA <phrase>JPA</phrase>. Browse through this guide to
+ familiarize yourself with the many advanced features and
+ customization opportunities OpenJPA provides. Later, you can use
+ the guide when you need details on a specific aspect of OpenJPA
+ <phrase>JPA</phrase>.
+ </para>
</listitem>
</itemizedlist>
</section>
@@ -72,35 +71,35 @@
<chapter id="jpa_overview_intro">
<title>Introduction</title>
<para><indexterm><primary>EJB3 Persistence</primary><see>EJB</see></indexterm><indexterm><primary>EJB</primary></indexterm>
- Enterprise Java Beans 3.0 Persistence (EJB persistence) is a specification
- from Sun Microsystems for the persistence of Java objects to any relational
- datastore. EJB persistence requires J2SE 1.5 (also referred to as "Java 5")
- or higher, as it makes heavy use of new Java language features such as
- annotations and generics. This document provides an overview of EJB
- persistence. Unless otherwise noted, the information presented
- applies to all EJB persistence implementations.
- </para>
+ Enterprise Java Beans 3.0 Persistence (EJB persistence) is a specification
+ from Sun Microsystems for the persistence of Java objects to any relational
+ datastore. EJB persistence requires J2SE 1.5 (also referred to as "Java 5")
+ or higher, as it makes heavy use of new Java language features such as
+ annotations and generics. This document provides an overview of EJB
+ persistence. Unless otherwise noted, the information presented
+ applies to all EJB persistence implementations.
+ </para>
<note>
<para>
- This document describes the Public Draft of the EJB 3.0
- persistence specification.
- </para>
- <para>
- For coverage of OpenJPA's many extensions to the EJB persistence
- specification, see the <link linkend="ref_guide_intro">Reference
- Guide</link>.
- </para>
+ This document describes the Public Draft of the EJB 3.0
+ persistence specification.
+ </para>
+ <para>
+ For coverage of OpenJPA's many extensions to the EJB persistence
+ specification, see the <link linkend="ref_guide_intro">Reference
+ Guide</link>.
+ </para>
</note>
<section id="jpa_overview_intro_audience">
<title>Intended Audience</title>
<para>
- This document is intended for developers who want to learn about
- EJB persistence in order to use it in their applications.
- It assumes that you have a strong knowledge of object-oriented concepts
- and Java, including Java 5 annotations and generics. It also assumes
- some experience with relational databases and the
- Structured Query Language (SQL).
- </para>
+ This document is intended for developers who want to learn about
+ EJB persistence in order to use it in their applications.
+ It assumes that you have a strong knowledge of object-oriented concepts
+ and Java, including Java 5 annotations and generics. It also assumes
+ some experience with relational databases and the
+ Structured Query Language (SQL).
+ </para>
</section>
<section id="jpa_overview_intro_transpers">
<title>Lightweight Persistence</title>
@@ -108,21 +107,21 @@
<primary>lightweight persistence</primary>
</indexterm>
<para><indexterm><primary>persistent data</primary></indexterm><emphasis>Persistent data</emphasis> is information that can
- outlive the program that creates it. The majority of complex
- programs use persistent data: GUI applications need to store user
- preferences across program invocations, web applications track
- user movements and orders over long periods of time, etc.
- </para>
+ outlive the program that creates it. The majority of complex
+ programs use persistent data: GUI applications need to store user
+ preferences across program invocations, web applications track
+ user movements and orders over long periods of time, etc.
+ </para>
<para><emphasis>Lightweight persistence</emphasis> is the storage and
- retrieval of persistent data with little or no work from you, the
- developer. For example, Java serialization<indexterm><primary>serialization</primary></indexterm> is a form of
- lightweight persistence because it can be used to persist Java
- objects directly to a file with very little effort. Serialization's
- capabilities as a lightweight persistence mechanism pale in
- comparison to those provided by EJB, however. The next
- chapter compares EJB to serialization and other available
- persistence mechanisms.
- </para>
+ retrieval of persistent data with little or no work from you, the
+ developer. For example, Java serialization<indexterm><primary>serialization</primary></indexterm> is a form of
+ lightweight persistence because it can be used to persist Java
+ objects directly to a file with very little effort. Serialization's
+ capabilities as a lightweight persistence mechanism pale in
+ comparison to those provided by EJB, however. The next
+ chapter compares EJB to serialization and other available
+ persistence mechanisms.
+ </para>
</section>
</chapter>
<chapter id="jpa_overview_why">
@@ -132,15 +131,15 @@
<secondary>why</secondary>
</indexterm>
<para>
- Java developers who need to store and retrieve persistent
- data already have several options available to them:
- serialization, JDBC, JDO, proprietary object-relational mapping tools,
- object databases, and EJB 2 entity beans. Why introduce yet
- another persistence framework? The answer to this question is that with
- the exception of JDO, each of the aforementioned persistence solutions
- has severe limitations. JPA attempts to overcome these
- limitations, as illustrated by the table below.
- </para>
+ Java developers who need to store and retrieve persistent
+ data already have several options available to them:
+ serialization, JDBC, JDO, proprietary object-relational mapping tools,
+ object databases, and EJB 2 entity beans. Why introduce yet
+ another persistence framework? The answer to this question is that with
+ the exception of JDO, each of the aforementioned persistence solutions
+ has severe limitations. JPA attempts to overcome these
+ limitations, as illustrated by the table below.
+ </para>
<table tocentry="1">
<title>Persistence Mechanisms</title>
<tgroup cols="8" align="left" colsep="1" rowsep="1">
@@ -295,8 +294,8 @@
</row>
<row>
<entry colname="sup">
- Relational and Non-Relational Stores
- </entry>
+ Relational and Non-Relational Stores
+ </entry>
<entry colname="ser">No</entry>
<entry colname="jdbc">No</entry>
<entry colname="or">No</entry>
@@ -377,123 +376,123 @@
<itemizedlist>
<listitem>
<para><indexterm><primary>serialization</primary></indexterm><indexterm><primary>JPA</primary><secondary>vs serialization</secondary></indexterm><emphasis>Serialization</emphasis> is Java's
- built-in mechanism for transforming an object graph into a
- series of bytes, which can then be sent over the network or
- stored in a file. Serialization is very easy to use,
- but it is also very limited. It must store and retrieve the
- entire object graph at once, making it unsuitable for
- dealing with large amounts of data. It cannot undo changes
- that are made to objects if an error occurs while updating
- information, making it unsuitable for applications that
- require strict data integrity. Multiple threads or programs
- cannot read and write the same serialized data concurrently
- without conflicting with each other. It provides no query
- capabilities. All these factors make serialization useless
- for all but the most trivial persistence needs.
- </para>
+ built-in mechanism for transforming an object graph into a
+ series of bytes, which can then be sent over the network or
+ stored in a file. Serialization is very easy to use,
+ but it is also very limited. It must store and retrieve the
+ entire object graph at once, making it unsuitable for
+ dealing with large amounts of data. It cannot undo changes
+ that are made to objects if an error occurs while updating
+ information, making it unsuitable for applications that
+ require strict data integrity. Multiple threads or programs
+ cannot read and write the same serialized data concurrently
+ without conflicting with each other. It provides no query
+ capabilities. All these factors make serialization useless
+ for all but the most trivial persistence needs.
+ </para>
</listitem>
<listitem>
<para><indexterm><primary>Java Database Connectivity</primary><see>JDBC</see></indexterm><indexterm><primary>JDBC</primary></indexterm><indexterm><primary>JPA</primary><secondary>vs JDBC</secondary></indexterm>
- Many developers use the
- <emphasis>Java Database Connectivity</emphasis> (JDBC) APIs to
- manipulate persistent data in relational databases. JDBC
- overcomes most of the shortcomings of serialization:
- it can handle large amounts of data, has mechanisms to ensure
- data integrity, supports concurrent access to information, and
- has a sophisticated query language in SQL. Unfortunately, JDBC
- does not duplicate serialization's ease of use. The relational
- paradigm used by JDBC was not designed for storing objects,
- and therefore forces you to either abandon
- object-oriented programming for the portions of your code
- that deal with persistent data, or to find a way of mapping
- object-oriented concepts like inheritance to relational
- databases yourself.
- </para>
+ Many developers use the
+ <emphasis>Java Database Connectivity</emphasis> (JDBC) APIs to
+ manipulate persistent data in relational databases. JDBC
+ overcomes most of the shortcomings of serialization:
+ it can handle large amounts of data, has mechanisms to ensure
+ data integrity, supports concurrent access to information, and
+ has a sophisticated query language in SQL. Unfortunately, JDBC
+ does not duplicate serialization's ease of use. The relational
+ paradigm used by JDBC was not designed for storing objects,
+ and therefore forces you to either abandon
+ object-oriented programming for the portions of your code
+ that deal with persistent data, or to find a way of mapping
+ object-oriented concepts like inheritance to relational
+ databases yourself.
+ </para>
</listitem>
<listitem>
<para><indexterm><primary>object-relational mapping</primary><see>ORM</see></indexterm><indexterm><primary>ORM</primary></indexterm><indexterm><primary>JPA</primary><secondary>vs ORM products</secondary></indexterm>
- There are many proprietary software products that can perform the
- mapping between objects and relational database tables for you.
- These <emphasis>object-relational mapping</emphasis> (ORM)
- frameworks allow you to focus on the object model and not concern
- yourself with the mismatch between
- the object-oriented and relational paradigms. Unfortunately,
- each of these product has its own set of APIs.
- Your code becomes tied to the proprietary interfaces of a single
- vendor. If the vendor raises prices, fails to fix show-stopping
- bugs, or falls behind in features, you cannot switch to another
- product without rewriting all of your persistence code. This is
- referred to as vendor lock-in.
- </para>
+ There are many proprietary software products that can perform the
+ mapping between objects and relational database tables for you.
+ These <emphasis>object-relational mapping</emphasis> (ORM)
+ frameworks allow you to focus on the object model and not concern
+ yourself with the mismatch between
+ the object-oriented and relational paradigms. Unfortunately,
+ each of these product has its own set of APIs.
+ Your code becomes tied to the proprietary interfaces of a single
+ vendor. If the vendor raises prices, fails to fix show-stopping
+ bugs, or falls behind in features, you cannot switch to another
+ product without rewriting all of your persistence code. This is
+ referred to as vendor lock-in.
+ </para>
</listitem>
<listitem>
<para><indexterm><primary>object database</primary><see>ODB</see></indexterm><indexterm><primary>ODB</primary></indexterm><indexterm><primary>JPA</primary><secondary>vs ODBs</secondary></indexterm><indexterm><primary>ODBMG</primary></indexterm>
- Rather than map objects to relational databases, some software
- companies have developed a form of database designed
- specifically to store objects. These
- <emphasis>object databases</emphasis> (ODBs) are often much
- easier to use than object-relational mapping software.
- The Object Database Management Group (ODMG) was formed to create
- a standard API for accessing object databases; few object
- database vendors, however, comply with the ODMG's
- recommendations. Thus, vendor lock-in plagues object databases
- as well. Many companies are also hesitant to switch from
- tried-and-true relational systems to the relatively unknown object
- database technology. Fewer data-analysis tools are available
- for object database systems, and there are vast quantities of
- data already stored in older relational databases. For all of
- these reasons and more, object databases have not caught on
- as well as their creators hoped.
- </para>
+ Rather than map objects to relational databases, some software
+ companies have developed a form of database designed
+ specifically to store objects. These
+ <emphasis>object databases</emphasis> (ODBs) are often much
+ easier to use than object-relational mapping software.
+ The Object Database Management Group (ODMG) was formed to create
+ a standard API for accessing object databases; few object
+ database vendors, however, comply with the ODMG's
+ recommendations. Thus, vendor lock-in plagues object databases
+ as well. Many companies are also hesitant to switch from
+ tried-and-true relational systems to the relatively unknown object
+ database technology. Fewer data-analysis tools are available
+ for object database systems, and there are vast quantities of
+ data already stored in older relational databases. For all of
+ these reasons and more, object databases have not caught on
+ as well as their creators hoped.
+ </para>
</listitem>
<listitem>
<para><indexterm><primary>Enterprise Java Beans</primary><see>EJB</see></indexterm><indexterm><primary>EJB</primary></indexterm><indexterm><primary>JPA</primary><secondary>vs EJB 2</secondary></indexterm>
- The Enterprise Edition of the Java platform introduced entity
- Enterprise Java Beans (EJBs). EJB 2.x entities are components that
- represent persistent information in a datastore. Like
- object-relational mapping solutions, EJB 2.x entities provide
- an object-oriented view of persistent data. Unlike
- object-relational software, however, EJB 2.x entities are not
- limited to relational databases; the persistent information they
- represent may come from an Enterprise Information System (EIS) or
- other storage device. Also, EJB 2.x entities use a strict standard,
- making them portable across vendors. Unfortunately, the EJB 2.x
- standard is somewhat limited in the object-oriented concepts it can
- represent. Advanced features like inheritance, polymorphism, and
- complex relations are absent. Additionally, EBJ 2.x entities are
- difficult to code, and they require heavyweight and often expensive
- application servers to run.
- </para>
+ The Enterprise Edition of the Java platform introduced entity
+ Enterprise Java Beans (EJBs). EJB 2.x entities are components that
+ represent persistent information in a datastore. Like
+ object-relational mapping solutions, EJB 2.x entities provide
+ an object-oriented view of persistent data. Unlike
+ object-relational software, however, EJB 2.x entities are not
+ limited to relational databases; the persistent information they
+ represent may come from an Enterprise Information System (EIS) or
+ other storage device. Also, EJB 2.x entities use a strict standard,
+ making them portable across vendors. Unfortunately, the EJB 2.x
+ standard is somewhat limited in the object-oriented concepts it can
+ represent. Advanced features like inheritance, polymorphism, and
+ complex relations are absent. Additionally, EBJ 2.x entities are
+ difficult to code, and they require heavyweight and often expensive
+ application servers to run.
+ </para>
</listitem>
<listitem>
<para><indexterm><primary>JDO</primary></indexterm><indexterm><primary>JPA</primary><secondary>vs JDO</secondary></indexterm>
- The JDO specification uses an API that is strikingly similar to
- JPA. JDO, however, supports non-relational databases,
- a feature that some argue dilutes the specification.
- </para>
+ The JDO specification uses an API that is strikingly similar to
+ JPA. JDO, however, supports non-relational databases,
+ a feature that some argue dilutes the specification.
+ </para>
</listitem>
</itemizedlist>
<para><indexterm><primary>JPA</primary></indexterm>
- JPA combines the best features from each of the persistence
- mechanisms listed above. Creating entities under JPA
- is as simple as creating serializable classes. JPA supports the
- large data sets, data consistency, concurrent use, and query capabilities of
- JDBC. Like object-relational software and object databases, JPA
- allows the use of advanced object-oriented concepts such as inheritance.
- JPA avoids vendor lock-in by relying on a strict specification
- like JDO and EJB 2.x entities. JPA focuses on relational
- databases. And like JDO, JPA is extremely easy to use.
- </para>
+ JPA combines the best features from each of the persistence
+ mechanisms listed above. Creating entities under JPA
+ is as simple as creating serializable classes. JPA supports the
+ large data sets, data consistency, concurrent use, and query capabilities of
+ JDBC. Like object-relational software and object databases, JPA
+ allows the use of advanced object-oriented concepts such as inheritance.
+ JPA avoids vendor lock-in by relying on a strict specification
+ like JDO and EJB 2.x entities. JPA focuses on relational
+ databases. And like JDO, JPA is extremely easy to use.
+ </para>
<note>
<para>
- OpenJPA typically stores data in relational databases, but can be
- customized for use with non-relational datastores as well.
- </para>
+ OpenJPA typically stores data in relational databases, but can be
+ customized for use with non-relational datastores as well.
+ </para>
</note>
<para>
- JPA is not ideal for every application. For many applications,
- though, it provides an exciting alternative to other persistence mechanisms.
- </para>
+ JPA is not ideal for every application. For many applications,
+ though, it provides an exciting alternative to other persistence mechanisms.
+ </para>
</chapter>
<chapter id="jpa_overview_arch">
<title>EJB Persistence Architecture</title>
@@ -502,9 +501,9 @@
<secondary>architecture</secondary>
</indexterm>
<para>
- The diagram below illustrates the relationships between the primary
- components of the EJB architecture.
- </para>
+ The diagram below illustrates the relationships between the primary
+ components of the EJB architecture.
+ </para>
<mediaobject>
<imageobject>
<!-- PNG image data 400 x 256 (see README) -->
@@ -516,78 +515,78 @@
</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>
+ 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>javax.persistence.Persistence</classname> class
- contains static helper methods to obtain
- <classname>EntityManagerFactory</classname> instances in a
- vendor-neutral fashion.
- </para>
+ The <classname>javax.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>javax.persistence.EntityManagerFactory
- </classname> class is a factory for <classname>
- EntityManager</classname>s.
- </para>
+ </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>javax.persistence.EntityManager</classname> is the
- primary EJB persistence 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>
+ The <classname>javax.persistence.EntityManager</classname> is the
+ primary EJB persistence 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>:
- Entites are persistent objects that represent datastore records.
- </para>
+ Entites 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>javax.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>
+ Each <classname>EntityManager</classname> has a one-to-one
+ relation with a single
+ <classname>javax.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>EJB3 Persistence Query Language</primary><see>JPQL</see></indexterm><indexterm><primary>JPQL</primary></indexterm><indexterm><primary>EJB</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>javax.persistence.Query</classname> interface is
- implemented by each EJB vendor to find persistent objects
- that meet certain criteria. EJB standardizes support
- for queries using both the EJB Query Language (JPQL) and
- the Structured Query Language (SQL). You obtain
- <classname>Query</classname> instances from an
- <classname>EntityManager</classname>.
- </para>
+ <classname>javax.persistence.Query</classname> interface is
+ implemented by each EJB vendor to find persistent objects
+ that meet certain criteria. EJB standardizes support
+ for queries using both the EJB 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 EJB interfaces interact to
- execute an JPQL query and update persistent objects. The example
- assumes execution outside a container.
- </para>
+ The example below illustrates how the EJB interfaces interact to
+ execute an 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 format="linespecific">
@@ -622,10 +621,10 @@
</programlisting>
</example>
<para>
- Within a container, the <classname>EntityManager</classname> will be
- injected and transactional handled declaratively. Thus, the in-container
- version of the example consists entirely of business logic:
- </para>
+ Within a container, the <classname>EntityManager</classname> will be
+ injected and transactional 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 format="linespecific">
@@ -645,10 +644,10 @@
</programlisting>
</example>
<para>
- The remainder of this document explores the EJB interfaces in
- detail. We present them in roughly the order that you will use them as you
- develop your application.
- </para>
+ The remainder of this document explores the EJB 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>EJB Exceptions</title>
<indexterm zone="jpa_overview_arch_exceptions">
@@ -670,23 +669,23 @@
</textobject>
</mediaobject>
<para>
- The diagram above depicts the EJB persistence exception architecture.
- All exceptions are unchecked. EJB persistence uses
- standard exceptions where appropriate, most notably <classname>
- IllegalArgumentException</classname>s and <classname>
- IllegalStateException</classname>s. The specification also provides
- a few EJB-specific exceptions in the <literal>javax.persistence
- </literal> package. These exceptions should be self-explanatory. See
- the <ulink url="http://java.sun.com/javaee/5/docs/api">Javadoc</ulink> for
- additional details on EJB exceptions.
- </para>
+ The diagram above depicts the EJB persistence exception architecture.
+ All exceptions are unchecked. EJB persistence uses
+ standard exceptions where appropriate, most notably <classname>
+ IllegalArgumentException</classname>s and <classname>
+ IllegalStateException</classname>s. The specification also provides
+ a few EJB-specific exceptions in the <literal>javax.persistence
+ </literal> package. These exceptions should be self-explanatory. See
+ the <ulink url="http://java.sun.com/javaee/5/docs/api">Javadoc</ulink> for
+ additional details on EJB 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>
+ 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>
@@ -710,35 +709,35 @@
<see>Entity</see>
</indexterm>
<para>
- JPA recognizes two types of persistent classes: <emphasis>
- entity</emphasis> classes and <emphasis>embeddable</emphasis> classes.
- Each persistent instance of an entity class - each
- <emphasis>entity</emphasis> - represents a unique datastore
- record. You can use the <classname>EntityManager</classname> to find an
- entity by its persistent identity (covered later in this chapter), or use
- a <classname>Query</classname> to find entities matching certain criteria.
- </para>
+ JPA recognizes two types of persistent classes: <emphasis>
+ entity</emphasis> classes and <emphasis>embeddable</emphasis> classes.
+ Each persistent instance of an entity class - each
+ <emphasis>entity</emphasis> - represents a unique datastore
+ record. You can use the <classname>EntityManager</classname> to find an
+ entity by its persistent identity (covered later in this chapter), or use
+ a <classname>Query</classname> to find entities matching certain criteria.
+ </para>
<para>
- An instance of an embeddable class, on the other hand, is only stored
- as part of a separate entity. Embeddable instances have no
- persistent identity, and are never returned directly from the
- <classname>EntityManager</classname> or from a <classname>Query</classname>.
- </para>
+ An instance of an embeddable class, on the other hand, is only stored
+ as part of a separate entity. Embeddable instances have no
+ persistent identity, and are never returned directly from the
+ <classname>EntityManager</classname> or from a <classname>Query</classname>.
+ </para>
<para>
- Despite these differences, there are few differences between entity classes
- and embeddable classes. In fact, writing either type
- of persistent class is little different than writing any other
- class. There are no special parent classes to extend from, field types to
- use, or methods to write. This is one important way in which JPA makes
- persistence transparent to you, the developer.
- </para>
+ Despite these differences, there are few differences between entity classes
+ and embeddable classes. In fact, writing either type
+ of persistent class is little different than writing any other
+ class. There are no special parent classes to extend from, field types to
+ use, or methods to write. This is one important way in which JPA makes
+ persistence transparent to you, the developer.
+ </para>
<note>
<para>
- JPA supports both fields and JavaBean properties
- as persistent state. For simplicity, however, we will
- refer to all persistent state as persistent fields, unless we
- want to note a unique aspect of persistent properties.
- </para>
+ JPA supports both fields and JavaBean properties
+ as persistent state. For simplicity, however, we will
+ refer to all persistent state as persistent fields, unless we
+ want to note a unique aspect of persistent properties.
+ </para>
</note>
<example id="jpa_overview_pc_pcclass">
<title>Persistent Class</title>
@@ -799,10 +798,10 @@
<secondary>restrictions on</secondary>
</indexterm>
<para>
- There are very few restrictions placed on persistent classes.
- Still, it never hurts to familiarize yourself with exactly what
- JPA does and does not support.
- </para>
+ There are very few restrictions placed on persistent classes.
+ Still, it never hurts to familiarize yourself with exactly what
+ JPA does and does not support.
+ </para>
<section id="jpa_overview_pc_no_arg">
<title>Default or No-Arg Constructor</title>
<indexterm zone="jpa_overview_pc_no_arg">
@@ -814,33 +813,33 @@
<secondary>no-arg constructor requirement</secondary>
</indexterm>
<para>
- The JPA specification requires that all persistent
- classes have a no-arg constructor. This constructor
- may be public or protected. Because the compiler automatically
- creates a default no-arg constructor when no other constructor
- is defined, only classes that define constructors must also
- include a no-arg constructor.
- </para>
+ The JPA specification requires that all persistent
+ classes have a no-arg constructor. This constructor
+ may be public or protected. Because the compiler automatically
+ creates a default no-arg constructor when no other constructor
+ is defined, only classes that define constructors must also
+ include a no-arg constructor.
+ </para>
<note>
<para>
- OpenJPA's <emphasis>enhancer</emphasis> will automatically add a
- protected no-arg constructor to your class when required.
- Therefore, this restriction does not apply when using
- OpenJPA. See <xref linkend="ref_guide_pc_enhance"/>
- of the Reference Guide for details.
- </para>
+ OpenJPA's <emphasis>enhancer</emphasis> will automatically add a
+ protected no-arg constructor to your class when required.
+ Therefore, this restriction does not apply when using
+ OpenJPA. See <xref linkend="ref_guide_pc_enhance"/>
+ of the Reference Guide for details.
+ </para>
</note>
</section>
<section id="jpa_overview_pc_final">
<title>Final</title>
<para>
- Entity classes may not be final. No method of an entity class can
- be final.
- </para>
+ Entity classes may not be final. No method of an entity class can
+ be final.
+ </para>
<note>
<para>
- OpenJPA supports final classes and final methods.
- </para>
+ OpenJPA supports final classes and final methods.
+ </para>
</note>
</section>
<section id="jpa_overview_pc_id">
@@ -854,25 +853,25 @@
<secondary>JPA id requirement</secondary>
</indexterm>
<para>
- All entity classes must declare one or more fields which together
- form the persistent identity of an instance. These are called
- <emphasis>identity</emphasis> or <emphasis>primary key</emphasis>
- fields. In our <classname>Magazine</classname> class,
- <literal>isbn</literal> and
- <literal>title</literal> are identity fields, because no two
- magazine records in the datastore can have the same
- <literal>isbn</literal> and <literal>title</literal> values.
- <xref linkend="jpa_overview_meta_id"/> will show you how to denote
- your identity fields in JPA metadata.
- <xref linkend="jpa_overview_pc_identity"/> below examines
- persistent identity.
- </para>
+ All entity classes must declare one or more fields which together
+ form the persistent identity of an instance. These are called
+ <emphasis>identity</emphasis> or <emphasis>primary key</emphasis>
+ fields. In our <classname>Magazine</classname> class,
+ <literal>isbn</literal> and
+ <literal>title</literal> are identity fields, because no two
+ magazine records in the datastore can have the same
+ <literal>isbn</literal> and <literal>title</literal> values.
+ <xref linkend="jpa_overview_meta_id"/> will show you how to denote
+ your identity fields in JPA metadata.
+ <xref linkend="jpa_overview_pc_identity"/> below examines
+ persistent identity.
+ </para>
<note>
<para>
- OpenJPA fully supports identity fields, but does not require them.
- See <xref linkend="ref_guide_pc_oid"/> of the Reference
- Guide for details.
- </para>
+ OpenJPA fully supports identity fields, but does not require them.
+ See <xref linkend="ref_guide_pc_oid"/> of the Reference
+ Guide for details.
+ </para>
</note>
</section>
<section id="jpa_overview_pc_version">
@@ -886,37 +885,37 @@
<secondary>JPA version requirement</secondary>
</indexterm>
<para>
- The <literal>version</literal> field in our <classname>Magazine
- </classname> class may seem out of place.
- JPA uses a version field in your entity to detect
- concurrent modifications to the same datastore record. When the
- JPA runtime detects an attempt to concurrently modify
- the same record, it throws an exception to the transaction
- attempting to commit last. This prevents overwriting the previous
- commit with stale data.
- </para>
- <para>
- The version field is not required, but without one concurrent
- threads or processes might succeed in making conflicting changes
- to the same record at the same time. This is unacceptable to most
- applications. <xref linkend="jpa_overview_meta_version"/>
- shows you how to designate a version field in JPA metadata.
- </para>
- <para>
- The version field must be an integral type (<classname>
- int</classname>, <classname>Long</classname>, etc) or a
- <classname>java.sql.Timestamp</classname>. You should consider
- version fields immutable. Changing the field value has undefined
- results.
- </para>
+ The <literal>version</literal> field in our <classname>Magazine
+ </classname> class may seem out of place.
+ JPA uses a version field in your entity to detect
+ concurrent modifications to the same datastore record. When the
+ JPA runtime detects an attempt to concurrently modify
+ the same record, it throws an exception to the transaction
+ attempting to commit last. This prevents overwriting the previous
+ commit with stale data.
+ </para>
+ <para>
+ The version field is not required, but without one concurrent
+ threads or processes might succeed in making conflicting changes
+ to the same record at the same time. This is unacceptable to most
+ applications. <xref linkend="jpa_overview_meta_version"/>
+ shows you how to designate a version field in JPA metadata.
+ </para>
+ <para>
+ The version field must be an integral type (<classname>
+ int</classname>, <classname>Long</classname>, etc) or a
+ <classname>java.sql.Timestamp</classname>. You should consider
+ version fields immutable. Changing the field value has undefined
+ results.
+ </para>
<note>
<para>
- OpenJPA fully supports version fields, but does not require them
- for concurrency detection. OpenJPA can maintain
- surrogate version values or use state comparisons to detect
- concurrent modifications. See
- <xref linkend="ref_guide_mapping_ejb"/> in the Reference Guide.
- </para>
+ OpenJPA fully supports version fields, but does not require them
+ for concurrency detection. OpenJPA can maintain
+ surrogate version values or use state comparisons to detect
+ concurrent modifications. See
+ <xref linkend="ref_guide_mapping_ejb"/> in the Reference Guide.
+ </para>
</note>
</section>
<section id="jpa_overview_pc_restrict_inheritance">
@@ -931,36 +930,36 @@
<secondary>of persistent classes</secondary>
</indexterm>
<para>
- JPA fully supports inheritance in persistent classes.
- It allows persistent classes to inherit from non-persistent classes,
- persistent classes to inherit from other persistent classes,
- and non-persistent classes to inherit from persistent classes.
- It is even possible to form inheritance hierarchies in which
- persistence skips generations. There are, however, a few
- important limitations:
- </para>
+ JPA fully supports inheritance in persistent classes.
+ It allows persistent classes to inherit from non-persistent classes,
+ persistent classes to inherit from other persistent classes,
+ and non-persistent classes to inherit from persistent classes.
+ It is even possible to form inheritance hierarchies in which
+ persistence skips generations. There are, however, a few
+ important limitations:
+ </para>
<itemizedlist>
<listitem>
<para>
- Persistent classes cannot inherit from certain
- natively-implemented system classes such as
- <classname>java.net.Socket</classname> and
- <classname>java.lang.Thread</classname>.
- </para>
+ Persistent classes cannot inherit from certain
+ natively-implemented system classes such as
+ <classname>java.net.Socket</classname> and
+ <classname>java.lang.Thread</classname>.
+ </para>
</listitem>
<listitem>
<para>
- If a persistent class inherits from a non-persistent
- class, the fields of the non-persistent superclass
- cannot be persisted.
- </para>
+ If a persistent class inherits from a non-persistent
+ class, the fields of the non-persistent superclass
+ cannot be persisted.
+ </para>
</listitem>
<listitem>
<para>
- All classes in an inheritance tree must use the same
- identity type. We cover entity identity in
- <xref linkend="jpa_overview_pc_identity"/>.
- </para>
+ All classes in an inheritance tree must use the same
+ identity type. We cover entity identity in
+ <xref linkend="jpa_overview_pc_identity"/>.
+ </para>
</listitem>
</itemizedlist>
</section>
@@ -981,37 +980,37 @@
<secondary>restrictions on</secondary>
</indexterm>
<para>
- JPA manages the state of all persistent fields.
- Before you access persistent state, the JPA runtime
- makes sure that it has been loaded from the datastore. When you
- set a field, the runtime records that it has changed so that
- the new value will be persisted. This allows you to treat the
- field in exactly the same way you treat any other field - another
- aspect of JPA's transparency.
- </para>
- <para>
- JPA does not support static or final fields.
- It does, however, include built-in support for most
- common field types. These types can be roughly divided into three
- categories: immutable types, mutable types, and relations.
- </para>
- <para><indexterm><primary>persistent fields</primary><secondary>immutable types</secondary></indexterm><indexterm><primary>immutable</primary><secondary>persistent field types</secondary></indexterm><emphasis>Immutable</emphasis> types, once created, cannot be
- changed. The only way to alter a persistent field of an immutable
- type is to assign a new value to the field. JPA
- supports the following immutable types:
- </para>
+ JPA manages the state of all persistent fields.
+ Before you access persistent state, the JPA runtime
+ makes sure that it has been loaded from the datastore. When you
+ set a field, the runtime records that it has changed so that
+ the new value will be persisted. This allows you to treat the
+ field in exactly the same way you treat any other field - another
+ aspect of JPA's transparency.
+ </para>
+ <para>
+ JPA does not support static or final fields.
+ It does, however, include built-in support for most
+ common field types. These types can be roughly divided into three
+ categories: immutable types, mutable types, and relations.
+ </para>
+ <para><indexterm><primary>persistent fields</primary><secondary>immutable types</secondary></indexterm><indexterm><primary>immutable</primary><secondary>persistent field types</secondary></indexterm><emphasis>Immutable</emphasis> types, once created, cannot be
+ changed. The only way to alter a persistent field of an immutable
+ type is to assign a new value to the field. JPA
+ supports the following immutable types:
+ </para>
<itemizedlist>
<listitem>
<para>
- All primitives (<classname>int, float, byte</classname>,
- etc)
- </para>
+ All primitives (<classname>int, float, byte</classname>,
+ etc)
+ </para>
</listitem>
<listitem>
<para>
- All primitive wrappers (<classname>java.lang.Integer,
- java.lang.Float, java.lang.Byte</classname>, etc)
- </para>
+ All primitive wrappers (<classname>java.lang.Integer,
+ java.lang.Float, java.lang.Byte</classname>, etc)
+ </para>
</listitem>
<listitem>
<para>
@@ -1030,20 +1029,20 @@
</listitem>
</itemizedlist>
<para>
- JPA also supports <classname>byte[]</classname> and
- <classname>char[]</classname> as immutable types. That is, you
- can persist fields of these types, but you should not manipulate
- individual array indexes without resetting the array into the
- persistent field.
- </para>
+ JPA also supports <classname>byte[]</classname> and
+ <classname>char[]</classname> as immutable types. That is, you
+ can persist fields of these types, but you should not manipulate
+ individual array indexes without resetting the array into the
+ persistent field.
+ </para>
<!-- ### EJB3 : Byte[], Character[] -->
<para><indexterm><primary>persistent fields</primary><secondary>mutable types</secondary><seealso>proxies</seealso></indexterm><indexterm><primary>mutable</primary><secondary>persistent field types</secondary><seealso>persistent fields</seealso><seealso>proxies</seealso></indexterm><indexterm><primary>persistent fields</primary><secondary>user-defined types</secondary></indexterm><indexterm><primary>user-defined</primary><secondary>persistent field types</secondary><seealso>persistent fields</seealso></indexterm>
- Persistent fields of <emphasis>mutable</emphasis> types
- can be altered without assigning the field a new value.
- Mutable types can be modified directly through their own
- methods. The JPA specification requires that
- implementations support the following mutable field types:
- </para>
+ Persistent fields of <emphasis>mutable</emphasis> types
+ can be altered without assigning the field a new value.
+ Mutable types can be modified directly through their own
+ methods. The JPA specification requires that
+ implementations support the following mutable field types:
+ </para>
<itemizedlist>
<listitem>
<para>
@@ -1076,59 +1075,59 @@
</listitem>
<listitem>
<para><classname>java.util.Collection</classname>s of entities
- </para>
+ </para>
</listitem>
<listitem>
<para><classname>java.util.Set</classname>s of entities
- </para>
+ </para>
</listitem>
<listitem>
<para><classname>java.util.List</classname>s of entities
- </para>
+ </para>
</listitem>
<listitem>
<para><classname>java.util.Map</classname>s in which each entry
- maps the value of one of an entity's fields to that entity.
- </para>
+ maps the value of one of an entity's fields to that entity.
+ </para>
</listitem>
</itemizedlist>
<para>
- Collection and map types may be parameterized.
- </para>
+ Collection and map types may be parameterized.
+ </para>
<para><indexterm><primary>persistent fields</primary><secondary>of unknown types</secondary></indexterm><indexterm><primary>Object</primary><secondary>as persistent field type</secondary><seealso>persistent fields</seealso></indexterm>
- Most JPA implementations also have support for
- persisting serializable values as binary data in the datastore.
- <xref linkend="jpa_overview_meta"/> has more information on
- persisting serializable types.
- </para>
+ Most JPA implementations also have support for
+ persisting serializable values as binary data in the datastore.
+ <xref linkend="jpa_overview_meta"/> has more information on
+ persisting serializable types.
+ </para>
<note>
<para>
- OpenJPA also supports arrays, <classname>
- java.lang.Number</classname>, <classname>
- java.util.Locale</classname>, all JDK 1.2 <classname>
- Set</classname>, <classname>List</classname>, and <classname>
- Map</classname> types, collections and maps of immutable and
- embedded as well as entity types, and many other mutable and
- immutable field types. OpenJPA also allows you to plug in
- support for custom types.
- </para>
+ OpenJPA also supports arrays, <classname>
+ java.lang.Number</classname>, <classname>
+ java.util.Locale</classname>, all JDK 1.2 <classname>
+ Set</classname>, <classname>List</classname>, and <classname>
+ Map</classname> types, collections and maps of immutable and
+ embedded as well as entity types, and many other mutable and
+ immutable field types. OpenJPA also allows you to plug in
+ support for custom types.
+ </para>
</note>
</section>
<section id="jpa_overview_pc_restrict_conclusion">
<title>Conclusions</title>
<para>
- This section detailed all of the restrictions JPA
- places on persistent classes. While it may seem like we presented
- a lot of information, you will seldom find yourself hindered by
- these restrictions in practice. Additionally, there are often ways
- of using JPA's other features to circumvent any
- limitations you run into.
- <!-- ### EJBDOC : good transition when lifecycle listeners are
- ### added back as next section
- The next section explores a powerful JPA feature
- that is particularly useful for this purpose.
- -->
- </para>
+ This section detailed all of the restrictions JPA
+ places on persistent classes. While it may seem like we presented
+ a lot of information, you will seldom find yourself hindered by
+ these restrictions in practice. Additionally, there are often ways
+ of using JPA's other features to circumvent any
+ limitations you run into.
+ <!-- ### EJBDOC : good transition when lifecycle listeners are
+ ### added back as next section
+ The next section explores a powerful JPA feature
+ that is particularly useful for this purpose.
+ -->
+ </para>
</section>
</section>
<section id="jpa_overview_pc_identity">
@@ -1147,86 +1146,86 @@
<secondary>JPA</secondary>
</indexterm>
<para><indexterm><primary>identity</primary><secondary>numeric</secondary></indexterm><indexterm><primary>identity</primary><secondary>qualitative</secondary></indexterm><indexterm><primary>numeric identity</primary><seealso>identity</seealso></indexterm><indexterm><primary>qualitative identity</primary><seealso>identity</seealso></indexterm>
- Java recognizes two forms of object identity: numeric identity and
- qualitative identity. If two references are
- <emphasis>numerically</emphasis> identical, then they refer to the
- same JVM instance in memory. You can test for this using the
- <literal>==</literal> operator. <emphasis>Qualitative</emphasis>
- identity, on the other hand, relies on some user-defined criteria to
- determine whether two objects are "equal". You test for qualitative
- identity using the <methodname>equals</methodname> method. By default,
- this method simply relies on numeric identity.
- </para>
- <para>
- JPA introduces another form of object identity, called
- <emphasis>entity identity</emphasis> or <emphasis>persistent
- identity</emphasis>. Entity identity tests whether two persistent
- objects represent the same state in the datastore.
- </para>
+ Java recognizes two forms of object identity: numeric identity and
+ qualitative identity. If two references are
+ <emphasis>numerically</emphasis> identical, then they refer to the
+ same JVM instance in memory. You can test for this using the
+ <literal>==</literal> operator. <emphasis>Qualitative</emphasis>
+ identity, on the other hand, relies on some user-defined criteria to
+ determine whether two objects are "equal". You test for qualitative
+ identity using the <methodname>equals</methodname> method. By default,
+ this method simply relies on numeric identity.
+ </para>
+ <para>
+ JPA introduces another form of object identity, called
+ <emphasis>entity identity</emphasis> or <emphasis>persistent
+ identity</emphasis>. Entity identity tests whether two persistent
+ objects represent the same state in the datastore.
+ </para>
<para><indexterm><primary>persistent fields</primary><secondary>id</secondary></indexterm><indexterm><primary>id</primary><secondary>fields</secondary><seealso>persistent fields</seealso></indexterm>
- The entity identity of each persistent instance is encapsulated in its
- <emphasis>identity field(s)</emphasis>. If two entities of the same
- type have the same identity field values, then the two
- entities represent the same state in the datastore. Each entity's
- identity field values must be unique among all other entites of the
- same type.
- </para>
- <para>
- Identity fields must be primitives, primitive wrappers,
- <classname>String</classname>s, <classname>Date</classname>s,
- <classname>Timestamp</classname>s, or embeddable types. Notably, other
- entities instances can <emphasis>not</emphasis> be used as identity
- fields.
- </para>
+ The entity identity of each persistent instance is encapsulated in its
+ <emphasis>identity field(s)</emphasis>. If two entities of the same
+ type have the same identity field values, then the two
+ entities represent the same state in the datastore. Each entity's
+ identity field values must be unique among all other entites of the
+ same type.
+ </para>
+ <para>
+ Identity fields must be primitives, primitive wrappers,
+ <classname>String</classname>s, <classname>Date</classname>s,
+ <classname>Timestamp</classname>s, or embeddable types. Notably, other
+ entities instances can <emphasis>not</emphasis> be used as identity
+ fields.
+ </para>
<note>
<para>
- For legacy schemas with binary primary key columns, OpenJPA
- also supports using identity fields of type
- <classname>byte[]</classname>. When you use a <classname>byte[]
- </classname> identity field, you must
- create an identity class. Identity classes are covered below.
- </para>
+ For legacy schemas with binary primary key columns, OpenJPA
+ also supports using identity fields of type
+ <classname>byte[]</classname>. When you use a <classname>byte[]
+ </classname> identity field, you must
+ create an identity class. Identity classes are covered below.
+ </para>
</note>
<warning>
<para>
- Changing the fields of an embeddable instance while it is assigned
- to an identity field has undefined results. Always treat
- embeddable identity instances as immutable objects in your
- applications.
- </para>
+ Changing the fields of an embeddable instance while it is assigned
+ to an identity field has undefined results. Always treat
+ embeddable identity instances as immutable objects in your
+ applications.
+ </para>
</warning>
<para><indexterm><primary>identity</primary><secondary>uniqueness requirement</secondary></indexterm><indexterm><primary>uniquness requirement</primary><seealso>identity</seealso></indexterm>
- If you are dealing with a single persistence context (see
- <xref linkend="jpa_overview_emfactory_perscontext"/>),
- then you do not have to compare identity fields to test
- whether two entity references represent the same state in the
- datastore. There is a much easier way: the
- <literal>==</literal> operator. JPA requires that
- each persistence context maintain only one JVM object to represent
- each unique datastore record. Thus, entity identity is equivalent to
- numeric identity within a persistence context. This is referred to as
- the <emphasis>uniqueness requirement</emphasis>.
- </para>
- <para>
- The uniqueness requirement is extremely important - without it, it
- would be impossible to maintain data integrity. Think of what
- could happen if two different objects in the same transaction
- were allowed to represent the same persistent data. If you made
- different modifications to each of these objects, which set of changes
- should be written to the datastore? How would your application logic
- handle seeing two different "versions" of the same data? Thanks to the
- uniqueness requirement, these questions do not have to be answered.
- </para>
+ If you are dealing with a single persistence context (see
+ <xref linkend="jpa_overview_emfactory_perscontext"/>),
+ then you do not have to compare identity fields to test
+ whether two entity references represent the same state in the
+ datastore. There is a much easier way: the
+ <literal>==</literal> operator. JPA requires that
+ each persistence context maintain only one JVM object to represent
+ each unique datastore record. Thus, entity identity is equivalent to
+ numeric identity within a persistence context. This is referred to as
+ the <emphasis>uniqueness requirement</emphasis>.
+ </para>
+ <para>
+ The uniqueness requirement is extremely important - without it, it
+ would be impossible to maintain data integrity. Think of what
+ could happen if two different objects in the same transaction
+ were allowed to represent the same persistent data. If you made
+ different modifications to each of these objects, which set of changes
+ should be written to the datastore? How would your application logic
+ handle seeing two different "versions" of the same data? Thanks to the
+ uniqueness requirement, these questions do not have to be answered.
+ </para>
<section id="jpa_overview_pc_identitycls">
<title>Identity Class</title>
<para><indexterm zone="jpa_overview_pc_identitycls"><primary>identity class</primary><seealso>identity</seealso></indexterm><indexterm zone="jpa_overview_pc_identitycls"><primary>identity</primary><secondary>class requirements</secondary></indexterm>
- If your entity has only one identity field, you can use the value of
- that field as the entity's identity object in all
- <link linkend="jpa_overview_em"><classname>EntityManager
- </classname></link> APIs. Otherwise, you must supply an identity
- class to use for identity objects. Your identity
- class must meet the following criteria:
- </para>
+ If your entity has only one identity field, you can use the value of
+ that field as the entity's identity object in all
+ <link linkend="jpa_overview_em"><classname>EntityManager
+ </classname></link> APIs. Otherwise, you must supply an identity
+ class to use for identity objects. Your identity
+ class must meet the following criteria:
+ </para>
<itemizedlist>
<listitem>
<para>The class must be public.</para>
@@ -1236,58 +1235,58 @@
</listitem>
<listitem>
<para>
- The class must have a public no-args constructor.
- </para>
+ The class must have a public no-args constructor.
+ </para>
</listitem>
<listitem>
<para>
- The names of the non-static fields or properties of the
- class must be the same as the names of the identity fields
- or properties of the corresponding entity class, and the
- types must be identical.
- </para>
+ The names of the non-static fields or properties of the
+ class must be the same as the names of the identity fields
+ or properties of the corresponding entity class, and the
+ types must be identical.
+ </para>
</listitem>
<listitem>
<para>
- The <methodname>equals</methodname> and
- <methodname>hashCode</methodname> methods of the class
- must use the values of all fields or properties
- corresponding to identity fields or properties in the
- entity class.
- </para>
+ The <methodname>equals</methodname> and
+ <methodname>hashCode</methodname> methods of the class
+ must use the values of all fields or properties
+ corresponding to identity fields or properties in the
+ entity class.
+ </para>
</listitem>
<listitem>
<para>
- If the class is an inner class, it must be
- <literal>static</literal>.
- </para>
+ If the class is an inner class, it must be
+ <literal>static</literal>.
+ </para>
</listitem>
<listitem>
<para>
- All entity classes related by inheritance must use the same
- identity class, or else each entity class must have its
- own identity class whose inheritance hierarchy mirrors the
- inheritance hierarchy of the owning entity classes (see
- <xref linkend="jpa_overview_pc_identity_hierarchy"/>).
- </para>
+ All entity classes related by inheritance must use the same
+ identity class, or else each entity class must have its
+ own identity class whose inheritance hierarchy mirrors the
+ inheritance hierarchy of the owning entity classes (see
+ <xref linkend="jpa_overview_pc_identity_hierarchy"/>).
+ </para>
</listitem>
</itemizedlist>
<note>
<para>
- Though you may still create identity classes by
- hand, OpenJPA provides the <classname>appidtool</classname>
- to automatically generate proper identity classes
- based on your identity fields. See
- <xref linkend="ref_guide_pc_oid_application"/> of the
- Reference Guide.
- </para>
+ Though you may still create identity classes by
+ hand, OpenJPA provides the <classname>appidtool</classname>
+ to automatically generate proper identity classes
+ based on your identity fields. See
+ <xref linkend="ref_guide_pc_oid_application"/> of the
+ Reference Guide.
+ </para>
</note>
<example id="jpa_overview_pc_identity_appidcode">
<title>Identity Class</title>
<para>
- This example illustrates a proper identity class for an entity
- with multiple identity fields.
- </para>
+ This example illustrates a proper identity class for an entity
+ with multiple identity fields.
+ </para>
<programlisting format="linespecific">
/**
* Persistent class using application identity.
@@ -1360,92 +1359,92 @@
</imageobject>
</mediaobject>
<para>
- An alternative to having a single identity
- class for an entire inheritance hierarchy is to have
- one identity class per level in the
- inheritance hierarchy. The requirements for using
- a hierarchy of identity classes are as follows:
- </para>
+ An alternative to having a single identity
+ class for an entire inheritance hierarchy is to have
+ one identity class per level in the
+ inheritance hierarchy. The requirements for using
+ a hierarchy of identity classes are as follows:
+ </para>
<itemizedlist>
<listitem>
<para>
- The inheritance hierarchy of identity
- classes must exactly mirror the hierarchy of the
- persistent classes that they identify. In the example
- pictured above, abstract class
- <classname>Person</classname> is extended by abstract
- class <classname>Employee</classname>, which is extended
- by non-abstract class <classname>
- FullTimeEmployee</classname>, which is extended by
- non-abstract class <classname>Manager</classname>.
- The corresponding identity classes, then, are
- an abstract <classname>PersonId</classname> class,
- extended by an abstract
- <classname>EmployeeId</classname> class, extended by a
- non-abstract <classname>FullTimeEmployeeId</classname>
- class, extended by a non-abstract
- <classname>ManagerId</classname> class.
- </para>
+ The inheritance hierarchy of identity
+ classes must exactly mirror the hierarchy of the
+ persistent classes that they identify. In the example
+ pictured above, abstract class
+ <classname>Person</classname> is extended by abstract
+ class <classname>Employee</classname>, which is extended
+ by non-abstract class <classname>
+ FullTimeEmployee</classname>, which is extended by
+ non-abstract class <classname>Manager</classname>.
+ The corresponding identity classes, then, are
+ an abstract <classname>PersonId</classname> class,
+ extended by an abstract
+ <classname>EmployeeId</classname> class, extended by a
+ non-abstract <classname>FullTimeEmployeeId</classname>
+ class, extended by a non-abstract
+ <classname>ManagerId</classname> class.
+ </para>
</listitem>
<listitem>
<para>
- Subclasses in the identity hierarchy
- may define additional identity fields until
- the hierarchy becomes non-abstract. In the
- aforementioned example, <classname>Person</classname>
- defines an identity field <literal>ssn</literal>,
- <classname>Employee</classname> defines additional
- identity field <literal>userName</literal>, and
- <classname>FullTimeEmployee</classname> adds a final
- identity field, <literal>empId</literal>.
- However, <classname>Manager</classname> may not define
- any additional identity fields, since it is a
- subclass of a non-abstract class. The hierarchy of
- identity classes, of course, must match the identity
- field definitions of the persistent class hierarchy.
- </para>
+ Subclasses in the identity hierarchy
+ may define additional identity fields until
+ the hierarchy becomes non-abstract. In the
+ aforementioned example, <classname>Person</classname>
+ defines an identity field <literal>ssn</literal>,
+ <classname>Employee</classname> defines additional
+ identity field <literal>userName</literal>, and
+ <classname>FullTimeEmployee</classname> adds a final
+ identity field, <literal>empId</literal>.
+ However, <classname>Manager</classname> may not define
+ any additional identity fields, since it is a
+ subclass of a non-abstract class. The hierarchy of
+ identity classes, of course, must match the identity
+ field definitions of the persistent class hierarchy.
+ </para>
</listitem>
<listitem>
<para>
- It is not necessary for each abstract class to declare
- identity fields. In the previous example, the
- abstract <classname>Person</classname> and
- <classname>Employee</classname> classes could declare
- no identity fields, and the first concrete subclass
- <classname>FullTimeEmployee</classname> could define
- one or more identity fields.
- </para>
+ It is not necessary for each abstract class to declare
+ identity fields. In the previous example, the
+ abstract <classname>Person</classname> and
+ <classname>Employee</classname> classes could declare
+ no identity fields, and the first concrete subclass
+ <classname>FullTimeEmployee</classname> could define
+ one or more identity fields.
+ </para>
</listitem>
<listitem>
<para>
- All subclasses of a concrete identity class must
- be <methodname>equals</methodname> and <methodname>
- hashCode</methodname>-compatible with the concrete
- superclass. This means that in our example, a
- <classname>ManagerId</classname> instance and a
- <classname>FullTimeEmployeeId</classname> instance
- with the same identity field values should have the
- same hash code, and should compare equal to each other
- using the <methodname>equals</methodname> method of
- either one. In practice, this requirement reduces to
- the following coding practices:
- </para>
+ All subclasses of a concrete identity class must
+ be <methodname>equals</methodname> and <methodname>
+ hashCode</methodname>-compatible with the concrete
+ superclass. This means that in our example, a
+ <classname>ManagerId</classname> instance and a
+ <classname>FullTimeEmployeeId</classname> instance
+ with the same identity field values should have the
+ same hash code, and should compare equal to each other
+ using the <methodname>equals</methodname> method of
+ either one. In practice, this requirement reduces to
+ the following coding practices:
+ </para>
<orderedlist>
<listitem>
<para>
- Use <literal>instanceof</literal> instead of
- comparing <classname>Class</classname> objects
- in the <methodname>equals</methodname> methods
- of your identity classes.
- </para>
+ Use <literal>instanceof</literal> instead of
+ comparing <classname>Class</classname> objects
+ in the <methodname>equals</methodname> methods
+ of your identity classes.
+ </para>
</listitem>
<listitem>
<para>
- An identity class that extends another
- non-abstract identity class should not override
- <methodname>equals</methodname> or
- <methodname>hashCode</methodname>.
- </para>
+ An identity class that extends another
+ non-abstract identity class should not override
+ <methodname>equals</methodname> or
+ <methodname>hashCode</methodname>.
+ </para>
</listitem>
</orderedlist>
</listitem>
@@ -1464,13 +1463,13 @@
<seealso>lifecycle callbacks</seealso>
</indexterm>
<para>
- It is often necessary to perform various actions at different stages
- of a persistent object's lifecycle. JPA includes
- a variety of callbacks methods for monitoring changes in the
- lifecycle of your persistent objects. These callbacks can
- be defined on the persistent classes themselves and
- on non-persistent listener classes.
- </para>
+ It is often necessary to perform various actions at different stages
+ of a persistent object's lifecycle. JPA includes
+ a variety of callbacks methods for monitoring changes in the
+ lifecycle of your persistent objects. These callbacks can
+ be defined on the persistent classes themselves and
+ on non-persistent listener classes.
+ </para>
<section id="jpa_overview_pc_callbacks_methods">
<title>Callback Methods</title>
<indexterm zone="jpa_overview_pc_callbacks_methods">
@@ -1482,104 +1481,104 @@
<secondary>callback methods</secondary>
</indexterm>
<para>
- Every persistence event has a corresponding callback method
- marker. These markers are shared between persistent classes
- and their listeners. You can use these markers to designate
- a method for callback either by annotating that method or by
- listing the method in the XML mapping file for a given class.
- The lifecycle events and their corresponding method markers are:
- </para>
+ Every persistence event has a corresponding callback method
+ marker. These markers are shared between persistent classes
+ and their listeners. You can use these markers to designate
+ a method for callback either by annotating that method or by
+ listing the method in the XML mapping file for a given class.
+ The lifecycle events and their corresponding method markers are:
+ </para>
<itemizedlist>
<listitem>
<para><indexterm><primary>PrePersist</primary><seealso>lifecycle callbacks</seealso></indexterm><ulink url="http://java.sun.com/javaee/5/docs/api/javax/persistence/PrePersist.html"><classname>PrePersist</classname></ulink>:
- Methods marked with this annotation will be invoked
- before an object is persisted. This could be used for
- assigning primary key values to persistent objects.
- This is equivalent to the XML element tag
- <literal>pre-persist</literal>.
- </para>
+ Methods marked with this annotation will be invoked
+ before an object is persisted. This could be used for
+ assigning primary key values to persistent objects.
+ This is equivalent to the XML element tag
+ <literal>pre-persist</literal>.
+ </para>
</listitem>
<listitem>
<para><indexterm><primary>PostPersist</primary><seealso>lifecycle callbacks</seealso></indexterm><ulink url="http://java.sun.com/javaee/5/docs/api/javax/persistence/PostPersist.html"><classname>PostPersist</classname></ulink>:
- Methods marked with this annotation will be invoked
- after an object has transitioned to the persistent state.
- You might want to use such methods to update a screen
- after a new row is added. This is equivalent
- to the XML element tag <literal>post-persist</literal>.
- </para>
+ Methods marked with this annotation will be invoked
+ after an object has transitioned to the persistent state.
+ You might want to use such methods to update a screen
+ after a new row is added. This is equivalent
+ to the XML element tag <literal>post-persist</literal>.
+ </para>
</listitem>
<listitem>
<para><indexterm><primary>PostLoad</primary><seealso>lifecycle callbacks</seealso></indexterm><ulink url="http://java.sun.com/javaee/5/docs/api/javax/persistence/PostLoad.html"><classname>PostLoad</classname></ulink>:
- Methods marked with this annotation will be invoked after
- all eagerly fetched fields of your class have been
- loaded from the datastore. No other persistent fields
- can be accessed in this method. This is equivalent
- to the XML element tag <literal>post-load</literal>.
- </para>
+ Methods marked with this annotation will be invoked after
+ all eagerly fetched fields of your class have been
+ loaded from the datastore. No other persistent fields
+ can be accessed in this method. This is equivalent
+ to the XML element tag <literal>post-load</literal>.
+ </para>
<para><classname>PostLoad</classname> is often used to
- initialize non-persistent fields whose values depend
- on the values of persistent fields, such as a complex
- datastructure.
- </para>
+ initialize non-persistent fields whose values depend
+ on the values of persistent fields, such as a complex
+ datastructure.
+ </para>
</listitem>
<listitem>
<para><indexterm><primary>PreUpdate</primary><seealso>lifecycle callbacks</seealso></indexterm><ulink url="http://java.sun.com/javaee/5/docs/api/javax/persistence/PreUpdate.html"><classname>PreUpdate</classname></ulink>:
- Methods marked with this annotation will be invoked
- just the persistent values in your objects are flushed
- to the datastore. This is equivalent to the XML element tag
- <literal>pre-update</literal>.
- </para>
+ Methods marked with this annotation will be invoked
+ just the persistent values in your objects are flushed
+ to the datastore. This is equivalent to the XML element tag
+ <literal>pre-update</literal>.
+ </para>
<para><classname>PreUpdate</classname> is the complement to
- <classname>PostLoad</classname>. While methods marked
- with <classname>PostLoad</classname> are most often used
- to initialize non-persistent values from persistent data,
- methods annotated with <classname>PreUpdate</classname>
- is normally used to set persistent fields with information
- cached in non-persistent data.
- </para>
+ <classname>PostLoad</classname>. While methods marked
+ with <classname>PostLoad</classname> are most often used
+ to initialize non-persistent values from persistent data,
+ methods annotated with <classname>PreUpdate</classname>
+ is normally used to set persistent fields with information
+ cached in non-persistent data.
+ </para>
</listitem>
<listitem>
<para><indexterm><primary>PostUpdate</primary><seealso>lifecycle callbacks</seealso></indexterm><ulink url="http://java.sun.com/javaee/5/docs/api/javax/persistence/PostUpdate.html"><classname>PostUpdate</classname></ulink>:
- Methods marked with this annotation will be invoked
- after changes to a given instance have been stored to the
- datastore. This is useful for clearing stale data cached
- at the application layer. This is equivalent to the
- XML element tag <literal>post-update</literal>.
- </para>
+ Methods marked with this annotation will be invoked
+ after changes to a given instance have been stored to the
+ datastore. This is useful for clearing stale data cached
+ at the application layer. This is equivalent to the
+ XML element tag <literal>post-update</literal>.
+ </para>
</listitem>
<listitem>
<para><indexterm><primary>PreRemove</primary><seealso>lifecycle callbacks</seealso></indexterm><ulink url="http://java.sun.com/javaee/5/docs/api/javax/persistence/PreRemove.html"><classname>PreRemove</classname></ulink>:
- Methods marked with this annotation will be invoked
- before an object transactions to the deleted state.
- Access to persistent fields is valid within this method.
- You might use this method to cascade the deletion to
- related objects based on complex criteria, or to
- perform other cleanup. This is equivalent to the
- XML element tag <literal>pre-remove</literal>.
- </para>
+ Methods marked with this annotation will be invoked
+ before an object transactions to the deleted state.
+ Access to persistent fields is valid within this method.
+ You might use this method to cascade the deletion to
+ related objects based on complex criteria, or to
+ perform other cleanup. This is equivalent to the
+ XML element tag <literal>pre-remove</literal>.
+ </para>
</listitem>
<listitem>
<para><indexterm><primary>PostRemove</primary><seealso>lifecycle callbacks</seealso></indexterm><ulink url="http://java.sun.com/javaee/5/docs/api/javax/persistence/PostRemove.html"><classname>PostRemove</classname></ulink>:
- Methods marked with this annotation will be invoked after
- an object has been marked as to be deleted. This is
- equivalent to the XML element tag
- <literal>post-remove</literal>.
- </para>
+ Methods marked with this annotation will be invoked after
+ an object has been marked as to be deleted. This is
+ equivalent to the XML element tag
+ <literal>post-remove</literal>.
+ </para>
</listitem>
</itemizedlist>
</section>
<section id="jpa_overview_callbacks_using">
<title>Using Callback Methods</title>
<para>
- When declaring callback methods on a persistent class,
- any method may be used which takes no arguments and is not
- shared with any property access fields. Multiple events
- can be assigned to a single method as well.
- </para>
- <para>
- Below is an example of how to declare callback methods
- on persistent classes:
- </para>
+ When declaring callback methods on a persistent class,
+ any method may be used which takes no arguments and is not
+ shared with any property access fields. Multiple events
+ can be assigned to a single method as well.
+ </para>
+ <para>
+ Below is an example of how to declare callback methods
+ on persistent classes:
+ </para>
<programlisting format="linespecific">
/**
* Example persistent class declaring our entity listener.
@@ -1611,9 +1610,9 @@
</programlisting>
<para>
- In an XML mapping file, we can define the same methods
- without annotations:
- </para>
+ In an XML mapping file, we can define the same methods
+ without annotations:
+ </para>
<programlisting format="linespecific">
<entity class="Magazine">
<pre-remove>logMagazineDeletion</pre-remove>
@@ -1622,33 +1621,33 @@
</programlisting>
<note>
<para>
- We fully explore persistence metadata annotations and XML in
- <xref linkend="jpa_overview_meta"/>.
- </para>
+ We fully explore persistence metadata annotations and XML in
+ <xref linkend="jpa_overview_meta"/>.
+ </para>
</note>
</section>
<section id="jpa_overview_entity_listeners_using">
<title>Using Entity Listeners</title>
<para>
- Mixing lifecycle event code into your persistent classes is not
- always ideal. It is often more elegant to handle cross-cutting
- lifecycle events in a non-persistent listener class.
[... 35382 lines stripped ...]