Apress Access

Pro JPA 2

Mastering the Java™ Persistence API

By Mike Keith , Merrick Schincariol

  • eBook Price: $34.99
Buy eBook Buy Print Book
Authors Mike Keith and Merrick Schincariol take a hands-on approach to teaching by giving examples to illustrate each concept of the Java Persistence API (JPA) and showing how it is used in practice.

Full Description

  • Add to Wishlist
  • ISBN13: 978-1-4302-1956-9
  • User Level: Intermediate
  • Publication Date: December 3, 2009
  • Available eBook Formats: EPUB, MOBI, PDF

Related Titles

  • Go Recipes
  • Beginning Hibernate, 4th Edition
  • Reactive Java Programming
  • Learning Groovy
Full Description

Pro JPA 2 introduces, explains, and demonstrates how to use the Java Persistence API (JPA). JPA provides Java developers with both the knowledge and insight needed to write Java applications that access relational databases through JPA.

Authors Mike Keith and Merrick Schincariol take a hands–on approach to teaching by giving examples to illustrate each concept of the API and showing how it is used in practice.

All of the examples use a common model from an overriding sample application, giving readers a context from which to start and helping them to understand the examples within an already familiar domain.

After completing the book, you will have a full understanding and be able to successfully code applications using JPA. The book also serves as a reference guide during initial and later JPA application experiences.

  • Hands-on examples for all the aspects of the JPA specification, based on the reference implementation of this specification
  • A special section on migration to JPA
  • Expert insight about various aspects of the API and when they are useful
  • Portability hints to provide increased awareness of the potential for non–portable JPA code

What you’ll learn

  • Get started with enterprise applications using JPA 2
  • Get up to speed with object–relational mapping and Entity Manager
  • Learn and use queries and the query language (JP QL)
  • Employ advanced object–relational mapping techniques
  • Use XML mapping files and learn other advanced techniques
  • Package and deploy your Java persistence applications
  • Test your Java persistence applications
  • Migrate your Java persistence applications

Who this book is for

The book generally targets enterprise and persistence developers who fall in one of three categories:

  • Those who are new to persistence; we will present some background information and introduce these readers to the basic persistence concepts to bring them up to speed.
  • Those who know and/or use existing ORM persistence products such as Hibernate or TopLink.
  • Those who have already used JPA and want to learn about newer features introduced by JPA 2, or have a good reference book to consult when they develop JPA applications.

In general, we assume that the reader is knowledgeable with Java, SQL, and JDBC, and has a little knowledge of J2EE.

Table of Contents

Table of Contents

  1. Introduction
  2. Getting Started
  3. Enterprise Applications
  4. Object-Relational Mapping
  5. Collection Mapping
  6. Entity Manager
  7. Using Queries
  8. Query Language
  9. Criteria API
  10. Advanced Object-Relational Mapping
  11. Advanced Topics
  12. XML Mapping Files
  13. Packaging and Deployment
  14. Testing
  15. Migration
Source Code/Downloads

Downloads are available to accompany this book.

Your operating system can likely extract zipped downloads automatically, but you may require software such as WinZip for PC, or StuffIt on a Mac.


If you think that you've found an error in this book, please let us know by emailing to editorial@apress.com . You will find any confirmed erratum below, so you can check if your concern has already been addressed.

On page 38:
listing 3, implement Logger should be extends Logger, it conflicts with the Logger.getLogger("Notification") line in the same list.

On page 50:

On line 29 , the injected EJB reference should be
but it is given
The full reference name is

On page 59:

"defined on java.transaction.Status" must read "defined on javax.transaction.Status"

On page 96:

The author references Figure 4-15 in the text, but I think he actually meant Figure 4-14.

Author Comment:

Yes. Thanks.

On page 99:
Figure 4-16: the left table must be EMPLOYEE, not PROJECT

On page 207:
In Chapter 8: Query Language, the jpqlExamples WAR has this query:

FROM Employee e JOIN e.projects p

When executed, the following error occurs:

java.lang.IllegalArgumentException: An exception occurred while creating a
query in EntityManager:
Exception Description: Error compiling the query [SELECT e, COUNT(p) FROM
Employee e JOIN e.projects p GROUP BY e HAVING COUNT(p) >= 2], line 1, column 80: invalid HAVING expression [COUNT(p) >= 2] for query with grouping [GROUP BY e]. The HAVING clause must specify search conditions over the grouping items or aggregate functions that apply to grouping items.

A EclipseLink bug was filed:
and the bug was closed with this comment:

Section 4.7 of the JPA 2.0 specification disallows using elements in the HAVING
clause that are not in the GROUP BY clause. I suggest raising an errata against the book.

Author Comment:

The spec is not as clear as it should be and may be open to some interpretation, but this was intended to be allowed. The EclipseLink bug should be reopened.

On page 207:

In Chapter's 8 examples source code there's an error in mapping between Employee and Phone entities. On the Employee's entity side the mapping is written well, but on the Phone's entity side it should be @ManyToOne instead of @OneToOne above the 'employee' field.

On page 261:

I found two issues in Chapter 9 criteria api, at the top of page 261:

1) The identification variable d in:
SELECT avg(d.salary) FROM e.directs is not defined.

2) The select clause of this same part of the query which is translated into a criteria query is missing in the criteria version also on top of page 261

On page 323:

The query on the top of the page with two left joins seems to be wrong based on the domain model in fig 8-1.

On page 340:

Listing 11-20:

ConstraintPaload got renamed into Payload for the final release of the Bean Validation spec.
Author Comment:

Yes. We apparently forgot to go back to change this example. Thanks for pointing it out.

On page 367:

In example: q.setProperty("javax.*", ..);

setProperty is not a valid method for the TypedQuery<?> q.
Author Comment:

Yes. We added get/setProperty to the EntityManager, but not to Query. In this example it should either have been either em.setProperty(...) or q.setHint(...)


On page 373:
Page 373, the xsd URL for JPA2 is incorrect. The link comes up as 404 not found.

Author Comment:
Not sure why you are getting a 404. The link is correct.


    1. Pro JavaFX 8


      View Book

    2. Practical Eclipse Rich Client Platform Projects


      View Book

    3. Pro Android 4


      View Book

    4. Pro Spring


      View Book