What is JPA (Java persistence API)? : JPA (Java persistence API) is not a tool or not a framework, JPA is a specification for accessing, persisting and managing data between Java Objects and Relational Database. The Java Persistence API now JPA 2.2 renamed to Jakarta Persistence in 2019.
- The Java Persistence API provides a POJO persistence model for object-relational mapping. The Java Persistence API was developed by the EJB 3.0 software expert group as part of JSR 220, but its use is not limited to EJB software components.
- JPA can be used in web applications, Java SE applications directly, even its part of EJB it’s not dpends on EJB containers.
- Technically JPA is just set of interfaces (Specification) and thus require an implementation.
- It needs ORM(Object Relational Mapping) implementation to work with and persist Java Objects.
- The API defined in
2. ORM – what is Object Relational Mapping?
ORM or Object Relational Mapping is a system that implements the responsibility of mapping the Object to Relational Model. That means it is responsible to store Object Model data into Relational Model and further read the data from Relational Model into Object Model. JPA requires an ORM implementation to work with relational model.
3. JPA Implementation
- There are several Object Relational Mapping implementations. Following are the popular ORM implementations.
- Hibernate – Open Source
- Top Link – By Oracle
- Eclipse Link – Eclipse Persistence Platform
- Open JPA – By Apache
- MyBatis – Open Source – Formerly known as iBATIS
- Many More …
- Eclipse Link (Eclipse Persistence Platform led by Oracle) Project as the reference implementation for the JPA 2.0 , JSR 317 Standard
Some popular implementations are Hibernate, EclipseLink, and Apache OpenJPA. JPA defines the metadata via annotations in Java classes or via XML configuration files. However, we can use both XML and annotation to describe the metadata.
4. JPA and Hibernate
- Hibernate is the popular and most advanced among all JPA implementations due to Red Hat.
- Hibernate ORM is one of the most mature JPA implementations, and still a popular option for ORM in Java. It uses its own tweaks and added features that may be used in addition to its JPA implementation. It has a bigger community of users and is well documented.
- Some of the additional proprietary features are support for multi-tenancy, inheritance, joining entities in queries, timestamp management, Hibernate Search, Hibernate Validator, and Hibernate OGM, which supports domain-model persistence for NoSQL.
5. JPA Features
JPA provides lot of features, following are the Java persistence api main features.
- Provides cleaner, easier, standardized object-relational mapping.
- JPA supports inheritance, polymorphism, and polymorphic queries.
- Supports meta data annotations/xml descriptors to define the mapping between Java objects and relational database.
- It supports a rich, SQL-like query language (which is a significant extension upon EJB QL) for both static and dynamic queries. Provides JPQL (Java persistence query language) and native SQL queries support.
- Provides support for stored procedures since 2.1.
- Pluggable persistence providers such as Hibernate, MyBatis etc.
- Criteria API, which supports to build SQL queries using OOPS, so that with the JPA criteria API it’s possible to have type safe queries that can be checked at compile time.
- Bean validation: the persistence provider enables Bean validation on all entities to which annotations are applied.
- Caching, JPA supports 2 kinds of cache first-level cache and second-level cache to support performance tuning.
- Better alignment with Java 8 features since JPA 2.2
In this article we have discussed what is JPA, popular implementations of Java persistence API and JPA main features.
- ORM (Object Relational Mapping)
- JPA Basic Terminology to Understand JPA Architecture
- Project configuration for JPA – To test sample applications
- How to persist an Entity in Database using JPA?