Data persistence with the Java Persistence API
The Java Persistence API (JPA) simplifies data persistence and object relational mapping for Java applications. With JPA, applications can efficiently create, read, update, and delete objects from a relational database.
Persistent data is data that outlives the application process that created it. Applications commonly persist data in relational databases, which organize data in sets of tables with columns and rows. Many different types of relational databases exist, and their configurations vary. JPA provides a standard for applications to interact with relational databases so that developers don’t need to worry about vendor-specific differences among databases or writing time-consuming boilerplate code to persist data.
What is JPA?
Enterprise applications require efficient access to relational databases. For example, an event calendar application needs to create, retrieve, update, and delete the events that users add to their calendars. The application itself can’t efficiently store this data and must be able to quickly retrieve it from a database, complete the requested function, and if necessary, return it to persistent storage. In the past, configuring these tasks required developers to manage complex SQL operations and differences among vendor databases. JPA provides an extra level of abstraction between the application and the database, enabling developers to manage data persistence in a standardized way.
JPA is a Jakarta EE specification to represent data in relational database tables as Plain Old Java Objects (POJO), a process that is known as object-relational mapping (ORM). JPA simplifies and standardizes ORM by using Java annotations or XML to map Java objects into one or more tables of a relational database. The JPA specification explicitly defines ORM, rather than relying on vendor-specific mapping implementations. JPA is based on the Java programming model that applies to Jakarta EE environments, but it can function within a Java SE environment for testing purposes.
JPA and JDBC
The Java Database Connectivity API (JDBC) handles the actual communication between an application and the tables in a relational database by managing SQL queries and commands. JPA provides a level of abstraction between the application and JDBC layers, allowing developers to manage persistence with Java annotations or XML, without having to worry about the details of SQL functions. Although managing persistence at the JDBC level provides more flexibility and fine-grained control over how data is persisted, it is much more complex to configure and maintain.
Whether you manage persistence through JPA or directly with JDBC depends on the needs of your application. Developers might prefer to work directly with JDBC if their application doesn’t have much interaction with a database or doesn’t need to support many different database vendors. However, working directly with JDBC requires familiarity with SQL. Developers who are not familiar with SQL, or whose applications need to support complex data models or multiple database vendors, might choose to manage persistence with JPA.
JPA configuration with Open Liberty
JPA for Open Liberty is supported by the JPA feature and the JPA Container feature. The JPA feature includes JPA specification interfaces and container-managed JPA integration. EclipseLink is included as the default JPA provider implementation. If you want to use a different provider implementation, such as Hibernate, enable the JPA Container feature. The JPA Container feature provides the same capabilities as the JPA feature but does not include a default provider implementation. If you enable the JPA feature, you do not need to explicitly enable the JPA Container feature. The JPA feature automatically enables the JPA Container feature.
For examples of how to configure a provider implementation other than the default, see the JPA Container feature.
Guide: Accessing and persisting data in microservices using JPA
Introduction to Jakarta Persistence from The Eclipse Foundation