Java Reference
In-Depth Information
Chapter 4
Object Relational Mapping and JPA
Object Relational Mapping (ORM) is the process of mapping entity classes to underlying database tables, and using
those entities to work with data, rather than direct SQL. Many developers have adopted ORM solutions over the years,
and a variety of different ORM technologies have emerged due to the uptake. The Java EE platform contains the Java
Persistence API (JPA), which is the standard ORM technology for Java enterprise development. Over the years, JPA has
changed significantly, making development easier and opening more options to developers with each release. The
latest release, JPA 2.1, is no exception, as it brings forth many incremental updates to the API, allowing developers
more flexibility and ease of development than ever before.
In this chapter, you will learn about the updates that have been made to JPA in an effort to expand the API.
Such updates include: ON Conditions, the ability to invoke database functions, bulk updates and deletions via the
Criteria API, and so on. You will also learn about enhancements to JPA that make development even easier and more
productive, such as schema generation.
Support for Joins with ON Conditions
When writing Java Persistence Query Language (JPQL) queries, it is sometimes beneficial to join two or more tables
to acquire related information. Furthermore, it is usually helpful to filter information based upon certain specified
criteria so that the number of records returned can be manageable. JPQL joins typically include
INNER
,
OUTER
, and
FETCH
joins. To review, an
INNER
join allows retrieval from two tables such that records being returned contain at
least one match in both tables. For instance, you may wish to query an
Employee
entity join it to
the Jobs
entity to
return only employees that have a specific job title. An
OUTER
join allows retrieval from two tables such that all of the
records from one of the entities (left entity) are returned, regardless if they match with a record in the other entity.
Lastly, a
FETCH
join enables the fetching of an association as a side effect of the query execution. In JPA 2.1, JPQL
has been updated to include the
ON
condition, which allows one to perform an
OUTER
join and include a specified
condition with the join. This capability has always been available with the
WHERE
clause of the JPQL query . . . but what
about the cases when you wish to return all matching records along with those that may not match, as with an
OUTER
join? The JPA 2.1 release provides this functionality in a concise manner with the addition of
ON
conditions. Simply
put, an
ON
condition modifies a join query such that it will incorporate better control over the data that is returned in
a concise manner.
In order to demonstrate this new syntax, let's take a look at an SQL query and then we will compare it to its JPQL
counterpart. The following SQL will join the
EMPLOYEE
table with the
JOBS
table on the
JOB_ID
field. It will also limit
the returned records to those that include a salary of >= 50,000 with the specification in the
WHERE
clause.
SELECT J.TITLE, COUNT(E.ID)
FROM JOBS J LEFT JOIN EMPLOYEE E
ON J.JOB_ID = E.JOB_ID and E.STATUS = 'ACTIVE'
WHERE J.SALARY >= 50000
GROUP BY J.TITLE;