Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

Java Persistence API (JPA) with EclipseLink - Tutorial

Lars Vogel

Version 0.9

13.10.2009

Revision History
Revision 0.103.02.2008Lars Vogel
OpenJPA
Revision 0.229.09.2008Lars Vogel
Added EclipseLink
Revision 0.316.10.2008Lars Vogel
Bug fixes
Revision 0.408.01.2009Lars Vogel
Reworked and cleaned up the article
Revision 0.521.01.2009Lars Vogel
Moved OpenJPA to another articles
Revision 0.622.01.2009Lars Vogel
Used JUnit for the example
Revision 0.703.02.2009Lars Vogel
Changed ID generation strategy to Table
Revision 0.830.05.2009Lars Vogel
Minor re-work
Revision 0.914.10.2009Lars Vogel
Improved JPA overview chapter, described persistence.xml parameters

JPA with Eclipselink

This article explains how to use EclipseLink, the reference implementation for the Java Persistence API (JPA). The usage of EclipseLink is demonstrated for stand-aloneJava applications (outside the Java EE environment).


Table of Contents

1. Java Persistence API
1.1. Overview
1.2. Persistence provider
2. Installation
2.1. EclipseLink
2.2. Derby Database
3. JPA with Annotations
3.1. Entity
3.2. Fields and Property setters and getters
3.3. Relationship Mapping
3.4. Entity Manager
3.5. Persistence Units
4. Example
4.1. Data model
4.2. Persistence Unit
4.3. Entity Manager
5. Thank you
6. Questions and Discussion
7. Appendix: Using JPA with XML metadata
7.1. XML Metadata
7.2. Example
8. Links and Literature
8.1. Java Persistence API Resources

1. Java Persistence API

1.1. Overview

The Java Persistence API (JPA) is a specification for persisting Java objects into a relational database. The Java Persistence API let the developer easily map, store, update and retrieve data from relational database tables into Java Objects and vice versa.

The Java Persistence API simplifies the life of a developer to a great degree. It permits the developer to work with objects rather then with SQL statements.

JPA can be used in Java-EE and Java-SE applications.

This process of mapping POJO's (Plain old Java Objects) to database tables and vice versa is called Object-relational mapping (ORM). This mapping is defined via persistence metadata, which defines the mapping to the database and the relationship between the objects.

This metadata can be specified using Java annotations or XML mapping files or a combination of both. If available the XML configuration overwrites the annotations in the Java code.

JPA also defines a powerful SQL-like Query language for static and dynamic queries.

JPA is a specification and for using JPA you always need a JPA provider.

The JPA provider will use the persistence metadata information to perform the correct database operations.

Most JPA persistence provider offer the option to create automatically the database schema based on the metadata.

1.2. Persistence provider

The persistence provider is the JPA implementation. The usage of JPA requires that an JPA provider implementation is added to the classpath.

Tip

I recommend to have only one persistence provider in your classpath. If you have more then one you can select the implementation via the provider tag to your persistence.xml, e.g. if you want to use EclipseLink:

						
<provider>org.eclipse.persistence.jpa.PersistenceProvider </provider>