What is JPA? Introduction to the Java Persistence API

The Java ORM standard for storing, accessing, and managing Java objects in a relational database

1 2 Page 2
Page 2 of 2

public class Musician {

  private List<Performance> performances = new ArrayList<Performance>();

One thing to notice is that the @JoinColumn tells JPA what column on the Performance table will map to the Musician entity. Each performance will be associated to a single Musician, which is tracked by this column. When JPA loads a Musician or a Performance into the database, it will use this information to reconstitute the object graph.

Fetching strategies in JPA

In addition to knowing where to place related entities in the database, JPA needs to know how you want them loaded. Fetching strategies tell JPA how to load related entities. When loading and saving objects, a JPA framework must provide the ability to finetune how object graphs are handled. For instance, if the Musician class has a bandMate field (as shown in Listing 7), loading george could cause the entire Musician table to be loaded from the database!

What is needed is the ability to define lazy loading of related entities--recognizing, of course, that relationships in JPA can be eager or lazy. You can use annotations to customize your fetching strategies, but JPA's default configuration often works out of the box, without changes:

  1. One-to-many: Lazy
  2. Many-to-one: Eager
  3. Many-to-many: Lazy
  4. One-to-one: Eager

JPA installation and setup

We'll conclude with a quick look at installing and setting up JPA for your Java applications. For this demonstration I'll use EclipseLink, the JPA reference implementation.

The common way to install JPA is to include a JPA provider into your project. Listing 8 shows how you would include EclipseLink as a dependency in your Maven pom.xml file.

Listing 8. Include EclipseLink as a Maven dependency


You will also need to include the driver for your database, as shown in Listing 9.

Listing 9. Maven dependency for a MySql connector


Next, you'll need to tell the system about your database and provider. This is done in the persistence.xml file, as shown in Listing 10.

Listing 10. Persistence.xml

http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
	<persistence-unit name="MyUnit" transaction-type="RESOURCE_LOCAL">
			<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/foo_bar"/>
			<property name="javax.persistence.jdbc.user" value=""/>
			<property name="javax.persistence.jdbc.password" value=""/>
			<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>

There are other ways to provide this information to the system, including programmatically. I recommend using the persistence.xml file because storing dependencies this way makes it very easy to update your application without modifying code.

Spring configuration for JPA

Using Spring will greatly ease the integration of JPA into your application. As an example, placing the @SpringBootApplication annotation in your application header instructs Spring to automatically scan for classes and inject the EntityManager as required, based on the configuration you've specified.

Listing 11 shows the dependencies to include if you want Spring's JPA support for your application.

Listing 11. Adding Spring JPA support in Maven




Every application that deals with a database should define an application layer whose sole purpose is to isolate persistence code. As you've seen in this article, the Java Persistence API introduces a range of capabilities and support for Java object persistence. Simple applications may not require all of JPA's capabilities, and in some cases the overhead of configuring the framework may not be merited. As an application grows, however, JPA's structure and encapsulation really earn their keep. Using JPA keeps your object code simple and provides a conventional framework for accessing data in Java applications.

Learn more about the Java Persistence API and related technologies

This story, "What is JPA? Introduction to the Java Persistence API" was originally published by JavaWorld.

Copyright © 2019 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2