Java Persistence 2.0 Expert Group
Specification Lead:
Linda DeMichiel, Sun Microsystems
Please send comments to: jsr-317-feedback@sun.com
F
in
a
l
R
e
l
e
a
s
e
Sun Microsystems
JSR 317: Java
TM
Persistence API, Version 2.0
November 10, 2009
Version 2.0, Final Release
Java Persistence 2.0, Final Release Sun Microsystems, Inc.
2 11/10/09
Specification: JSR-000317 Java(tm) Persistence 2.0 ("Specification")
Version: 2.0
Status: Final Release
Release: 10 December 2009
Copyright 2009 SUN MICROSYSTEMS, INC.
4150 Network Circle, Santa Clara, California 95054, U.S.A
All rights reserved.
LIMITED LICENSE GRANTS
1. License for Evaluation Purposes. Sun hereby grants you a fully-paid, non-exclusive, non-transferable,
worldwide, limited license (without the right to sublicense), under Sun's applicable intellectual property
rights to view, download, use and reproduce the Specification only for the purpose of internal evaluation.
This includes (i) developing applications intended to run on an implementation of the Specification, pro-
vided that such applications do not themselves implement any portion(s) of the Specification, and (ii) dis-
cussing the Specification with any third party; and (iii) excerpting brief portions of the Specification in
oral or written communications which discuss the Specification provided that such excerpts do not in the
aggregate constitute a significant portion of the Specification.
2. License for the Distribution of Compliant Implementations. Sun also grants you a perpetual, non-ex-
clusive, non-transferable, worldwide, fully paid-up, royalty free, limited license (without the right to sub-
license) under any applicable copyrights or, subject to the provisions of subsection 4 below, patent rights
it may have covering the Specification to create and/or distribute an Independent Implementation of the
Specification that: (a) fully implements the Specification including all its required interfaces and func-
tionality; (b) does not modify, subset, superset or otherwise extend the Licensor Name Space, or include
any public or protected packages, classes, Java interfaces, fields or methods within the Licensor Name
Space other than those required/authorized by the Specification or Specifications being implemented;
and (c) passes the Technology Compatibility Kit (including satisfying the requirements of the applicable
TCK Users Guide) for such Specification ("Compliant Implementation"). In addition, the foregoing li-
cense is expressly conditioned on your not acting outside its scope. No license is granted hereunder for
any other purpose (including, for example, modifying the Specification, other than to the extent of your
fair use rights, or distributing the Specification to third parties). Also, no right, title, or interest in or to
any trademarks, service marks, or trade names of Sun or Sun's licensors is granted hereunder. Java, and
Java-related logos, marks and names are trademarks or registered trademarks of Sun Microsystems, Inc.
in the U.S. and other countries.
3. Pass-through Conditions. You need not include limitations (a)-(c) from the previous paragraph or any
other particular "pass through" requirements in any license You grant concerning the use of your Inde-
pendent Implementation or products derived from it. However, except with respect to Independent Im-
plementations (and products derived from them) that satisfy limitations (a)-(c) from the previous
paragraph, You may neither: (a) grant or otherwise pass through to your licensees any licenses under
Sun's applicable intellectual property rights; nor (b) authorize your licensees to make any claims con-
cerning their implementation's compliance with the Specification in question.
4. Reciprocity Concerning Patent Licenses.
a. With respect to any patent claims covered by the license granted under subparagraph 2 above that
would be infringed by all technically feasible implementations of the Specification, such license is con-
ditioned upon your offering on fair, reasonable and non-discriminatory terms, to any party seeking it
Java Persistence 2.0, Final Release Sun Microsystems, Inc.
3 11/10/09
from You, a perpetual, non-exclusive, non-transferable, worldwide license under Your patent rights
which are or would be infringed by all technically feasible implementations of the Specification to de-
velop, distribute and use a Compliant Implementation.
b With respect to any patent claims owned by Sun and covered by the license granted under subpara-
graph 2, whether or not their infringement can be avoided in a technically feasible manner when imple-
menting the Specification, such license shall terminate with respect to such claims if You initiate a claim
against Sun that it has, in the course of performing its responsibilities as the Specification Lead, induced
any other entity to infringe Your patent rights.
c Also with respect to any patent claims owned by Sun and covered by the license granted under sub-
paragraph 2 above, where the infringement of such claims can be avoided in a technically feasible man-
ner when implementing the Specification such license, with respect to such claims, shall terminate if You
initiate a claim against Sun that its making, having made, using, offering to sell, selling or importing a
Compliant Implementation infringes Your patent rights.
5. Definitions. For the purposes of this Agreement: "Independent Implementation" shall mean an imple-
mentation of the Specification that neither derives from any of Sun's source code or binary code mate-
rials nor, except with an appropriate and separate license from Sun, includes any of Sun's source code
or binary code materials; "Licensor Name Space" shall mean the public class or interface declarations
whose names begin with "java", "javax", "com.sun" or their equivalents in any subsequent naming con-
vention adopted by Sun through the Java Community Process, or any recognized successors or replace-
ments thereof; and "Technology Compatibility Kit" or "TCK" shall mean the test suite and
accompanying TCK User's Guide provided by Sun which corresponds to the Specification and that was
available either (i) from Sun 120 days before the first release of Your Independent Implementation that
allows its use for commercial purposes, or (ii) more recently than 120 days from such release but against
which You elect to test Your implementation of the Specification.
This Agreement will terminate immediately without notice from Sun if you breach the Agreement or act
outside the scope of the licenses granted above.
DISCLAIMER OF WARRANTIES
THE SPECIFICATION IS PROVIDED "AS IS". SUN MAKES NO REPRESENTATIONS OR WAR-
RANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO, WARRAN-
TIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-
INFRINGEMENT (INCLUDING AS A CONSEQUENCE OF ANY PRACTICE OR IMPLEMENTA-
TION OF THE SPECIFICATION), OR THAT THE CONTENTS OF THE SPECIFICATION ARE
SUITABLE FOR ANY PURPOSE. This document does not represent any commitment to release or im-
plement any portion of the Specification in any product. In addition, the Specification could include tech-
nical inaccuracies or typographical errors.
LIMITATION OF LIABILITY
TO THE EXTENT NOT PROHIBITED BY LAW, IN NO EVENT WILL SUN OR ITS LICENSORS
BE LIABLE FOR ANY DAMAGES, INCLUDING WITHOUT LIMITATION, LOST REVENUE,
PROFITS OR DATA, OR FOR SPECIAL, INDIRECT, CONSEQUENTIAL, INCIDENTAL OR PU-
NITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILI-
TY, ARISING OUT OF OR RELATED IN ANY WAY TO YOUR HAVING, IMPLEMENTING OR
OTHERWISE USING USING THE SPECIFICATION, EVEN IF SUN AND/OR ITS LICENSORS
HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
Java Persistence 2.0, Final Release Sun Microsystems, Inc.
4 11/10/09
You will indemnify, hold harmless, and defend Sun and its licensors from any claims arising or resulting
from: (i) your use of the Specification; (ii) the use or distribution of your Java application, applet and/or
implementation; and/or (iii) any claims that later versions or releases of any Specification furnished to
you are incompatible with the Specification provided to you under this license.
RESTRICTED RIGHTS LEGEND
U.S. Government: If this Specification is being acquired by or on behalf of the U.S. Government or by a
U.S. Government prime contractor or subcontractor (at any tier), then the Government's rights in the
Software and accompanying documentation shall be only as set forth in this license; this is in accordance
with 48 C.F.R. 227.7201 through 227.7202-4 (for Department of Defense (DoD) acquisitions) and with
48 C.F.R. 2.101 and 12.212 (for non-DoD acquisitions).
REPORT
If you provide Sun with any comments or suggestions concerning the Specification ("Feedback"), you
hereby: (i) agree that such Feedback is provided on a non-proprietary and non-confidential basis, and (ii)
grant Sun a perpetual, non-exclusive, worldwide, fully paid-up, irrevocable license, with the right to sub-
license through multiple levels of sublicensees, to incorporate, disclose, and use without limitation the
Feedback for any purpose.
GENERAL TERMS
Any action related to this Agreement will be governed by California law and controlling U.S. federal law.
The U.N. Convention for the International Sale of Goods and the choice of law rules of any jurisdiction
will not apply.
The Specification is subject to U.S. export control laws and may be subject to export or import regula-
tions in other countries. Licensee agrees to comply strictly with all such laws and regulations and ac-
knowledges that it has the responsibility to obtain such licenses to export, re-export or import as may be
required after delivery to Licensee.
This Agreement is the parties' entire agreement relating to its subject matter. It supersedes all prior or
contemporaneous oral or written communications, proposals, conditions, representations and warranties
and prevails over any conflicting or additional terms of any quote, order, acknowledgment, or other com-
munication between the parties relating to its subject matter during the term of this Agreement. No mod-
ification to this Agreement will be binding, unless in writing and signed by an authorized representative
of each party.
Rev. April, 2006
Java Persistence 2.0, Final Release
5 11/10/09
Sun Microsystems, Inc.
Table of Contents
Chapter 1 Introduction.................................................................................................................... 19
1.1 Expert Group ................................................................................................... 19
1.2 Document Conventions ................................................................................... 19
Chapter 2 Entities ........................................................................................................................... 21
2.1 The Entity Class .............................................................................................. 21
2.2 Persistent Fields and Properties....................................................................... 22
2.2.1 Example ............................................................................................ 24
2.3 Access Type..................................................................................................... 25
2.3.1 Default Access Type ......................................................................... 25
2.3.2 Explicit Access Type......................................................................... 26
2.3.3 Access Type of an Embeddable Class............................................... 27
2.3.4 Defaulted Access Types of Embeddable Classes and Mapped Superclasses
27
2.4 Primary Keys and Entity Identity.................................................................... 27
2.4.1 Primary Keys Corresponding to Derived Identities.......................... 29
2.4.1.1 Specification of Derived Identities ..................................... 29
2.4.1.2 Mapping of Derived Identities............................................ 30
2.4.1.3 Examples of Derived Identities .......................................... 30
2.5 Embeddable Classes ........................................................................................ 38
2.6 Collections of Embeddable Classes and Basic Types ..................................... 39
2.7 Map Collections .............................................................................................. 39
2.7.1 Map Keys .......................................................................................... 39
2.7.2 Map Values........................................................................................ 40
2.8 Mapping Defaults for Non-Relationship Fields or Properties......................... 40
2.9 Entity Relationships ........................................................................................ 41
2.10 Relationship Mapping Defaults....................................................................... 43
2.10.1 Bidirectional OneToOne Relationships ............................................ 43
2.10.2 Bidirectional ManyToOne / OneToMany Relationships................... 44
2.10.3 Unidirectional Single-Valued Relationships ..................................... 46
2.10.3.1 Unidirectional OneToOne Relationships............................ 46
2.10.3.2 Unidirectional ManyToOne Relationships ......................... 47
2.10.4 Bidirectional ManyToMany Relationships ....................................... 48
2.10.5 Unidirectional Multi-Valued Relationships ...................................... 49
2.10.5.1 Unidirectional OneToMany Relationships ......................... 50
2.10.5.2 Unidirectional ManyToMany Relationships....................... 51
2.11 Inheritance ....................................................................................................... 52
2.11.1 Abstract Entity Classes ..................................................................... 52
2.11.2 Mapped Superclasses ........................................................................ 53
2.11.3 Non-Entity Classes in the Entity Inheritance Hierarchy................... 55
2.12 Inheritance Mapping Strategies....................................................................... 56
2.12.1 Single Table per Class Hierarchy Strategy........................................ 56
2.12.2 Joined Subclass Strategy ................................................................... 56