Imagine that you have to develop a note-taking application where users’ notes represent data. To store it in our app, we use relational databases. As you already know, relational databases consist of tables and relationships between them. In our application, we work with program language objects. To convert them into relational database tables, we use different ORM (object-relational mapping) solutions. There are quite a few such solutions, so a standard interface appeared. It’s called JPA (Java Persistence API), now known as Jakarta Persistence, and it’s a specification that describes how to make data persistent.

JPA standard

By default, JPA has three main parts:

  • API. The API defines a set of interfaces that allow you to organize the interaction with the ORM provider. The interfaces are located in the jakarta.persistence package after the 3.0 version of JPA. Before that, they used to be in the javax.persistence package.
  • JPQL (Java Persistence Query Language). It’s very similar to SQL but requests are made to objects.
  • Metadata. It is a set of annotations over objects, which we use to describe the metadata. It helps JPA to define a table to which each object is saved. We can describe our metadata in two ways: using an XML file or annotations.

Now let’s get familiar with some more basic concepts. To represent data from the database to objects, we use an entity, which is a simple class. The entity represents a database table, while the fields of the entity represent table columns, and each entity instance represents a table row. Entity follows some basic rules, for example, each entity must have a primary key that uniquely identifies the object. It must be annotated with the @Entity annotation or described in an XML JPA config file. Also, it must have a public or protected no-argument constructor. There are some other rules which we will discuss in the next topics.

In the image below, we can see how JPA helps us convert our objects to relational tables and back.

JPA implementations

JPA is a specification, which means that it needs implementation. JPA itself does not know how to save or manage objects, it only dictates the rules of the game. JPA defines the interfaces that will need to be implemented by providers, the rules for describing metadata, and how providers should work. Next, each JPA-implementing provider defines the receipt, storage, and management of objects. Each provider has a different implementation.

The most popular implementation is Hibernate. It provides a high-performance, reliable, extensible, and fully compatible implementation of JPA. Moreover, this framework has a huge community that makes it easy to find solutions to possible problems.

There are also other implementations, such as Oracle TopLinkEclipseLink, and Apache OpenJPA.

JPA and Spring/Spring Boot

Part of the Spring framework called Spring Data JPA is aimed at working with any relational databases by means of JPA. The main concept is a repository. There are several interfaces that use JPA Entity to interact with it. For example, the interface CrudRepository provides basic operations for searching, saving, and deleting data.

It is not necessary to use Spring together with JPA. You can always use JPA without Spring!

Here is what you need to do to enable JPA in a Spring Boot application for Gradle-based Spring Boot projects:

dependencies {
    // ...
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    // ...
}

For Maven-based Spring Boot projects:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

The spring-boot-starter-data-jpa contains the necessary auto-configuration for Spring JPA. Spring Boot configures Hibernate as the default JPA provider. Spring Boot can also auto-configure data sources.

Conclusion

JPA is a convenient way to deal with relational databases in JVM applications. In this introductory topic, you’ve learned what the JPA standard includes, its implementations, and the most popular of them, Hibernate. You’ve also learned that JPA has an entity for representing data from a database to objects, and studied the usage of JPA with Spring framework, where the main concept is a repository. We will focus on this term in the next topics.

Leave a Reply

Your email address will not be published.