JPA - Jakarta Persistence API
What is it?
JPA handles most of the complexity of JDBC-based database access and object-relational mappings.
It’s a specification which is part of Java EE and defines an API for object-relational mappings and for managing persistent objects. You can use this API in Java SE and Java EE environments.
Note: jakarta persistence api
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
JPA itself doesn’t provide any implementation classes. The API jar just contains a set of interfaces which you can use to implement your persistence layer. But you can’t use JPA on its own. You need a JPA provider which implements the specification. There are several options available. The most popular ones are Hibernate and EclipseLink.
What’s defined by the JPA specification
Here are some of the most important features defined by the specification.
Bootstrapping and basic entity mappings
Before you can start using JPA, you need to add it to your project, configure a persistence unit, map entities to your database tables and bootstrap it.
Mapping Associations
JPA doesn’t only enable you to map simple entity attributes to database columns, but it also allows you to map associations between database tables to entity attributes.
JPQL and Native Queries
Custom Data Types
The JPA specification defines the mapping for most standard types without limiting you to them. Since JPA 2.1, you can easily support custom data types with an AttributeConverter. You just need to implement the AttributeConverter interface and annotate the class with a @Converter annotation.
Implementation frameworks
- Spring Data JPA supports both JPQL and Native Query.
- Apache DeltaSpike Data: https://deltaspike.apache.org/documentation/data.html
Reading material
Tags
- JPA - Jakarta Persistence Query Language - JPQL
- JPA - native queries
- JPA - Composite Primary Keys
- JPA - Inheritance strategies
- JPA - entity associations and association mappings
- JPA - Repository pattern
- JPA - Providers
- JPA - Persistence, EntityManagerFactory, EntityManager and PersistenceContext
- JPA - Entity Object Life Cycle
- JPA - JOIN FETCH
- JPA - Avoiding the N+1 Problem