Spring Data JPA

It is an effective Spring Source project. Its motive is to integrate and simply access several types of persistence stores, both R-DBMS and NoSQL data stores.

When implementing a new application, you need to focus on business logic, not technical complexity or boilerplate code. For this reason, the Spring Data JPA and the Java Persistent API (JPA) description are in favoured.

That is, Spring Data JPA uses all the features defined in the JPA specification, especially JPA's entities, association mapping, and query features. Spring Data JPA adds unique features such as no-code implementation of repository patterns and database query creation from method names.

Objectives

Spring Data JPA tackles all of the difficulties of database access and ORM (object-relational mapping) based on the JDBC. This reduces the boilerplate code required for JPA. This makes implementing without efforts and faster for the persistence layer.

Spring Data JPA aims to brush up the execution of the data access layer by bringing down the required overhead.

Spring Data JPA Features

Spring Data JPA has three main features:

  • No-code repository: This is the most common persistence pattern. This allows you to implement your business code with a higher level of abstraction.
  • Boilerplate code reduction: Provides a default implementation of each method via the repository interface. This means that you don't have to implement read and write operations.
  • Generated Queries: One more characteristic provided by the Spring Data JPA that helps to generate database queries depending on the names of the method. If the query is not very complex, you need to define the method in the repository interface whose name starts with findBy. Once we define the method, Spring parses the name of the method and generates its query. 

For example:

public interface LibraryRepository extends CrudRepository<Books, Long>
{  
Books findByName(String name);  
}  

Conclusion: The above program extends CrudRepository, that uses two generics, Books and Long. Book is the managed entity and Long can be defined as the data type of the primary key.

Spring creates the JPQL (Java Persistence Query Language) queries internally depending on the name of the method. We could say that this query is procured from method signature. First, setting up the binding variables value, executes the query, and gives the result.

There are several more features, followings are some of them:

  • You can integrate custom repository code.
  • A significant repository and User-defined object mapping abstraction.
  • Holds up crystal clear auditing.
  • Executes a domain base class that is used to provide the basic properties.
  • Keep up several modules such as Spring Data JPA, Cassandra, MongoDB, REST and etc.

Spring Data Repository

Spring Data JPA provides the following three repositories:

  • CrudRepository: Provides standard creation, reading, updating, and deleting capabilities. It holds up methods such assave(), findAll(), findOne(),delete(), etc.
  • PagingAndSortingRepository: Extends CrudRepository and adds a findAll method. Enables us to fetch and classify the data in a paged format.
  • JpaRepository: This is a JPA-specific repository. Defined in Spring-Data-Jpa. It extends the two repositories PagingAndSortingRepository and CrudRepository. To trigger a flush in a persistent context, add a JPA-specific method such as flush ().
<dependency>  
<groupId>org.springframework.data</groupId>  
<artifactId>spring-data-jpa</artifactId>  
<version>2.2.3.RELEASE</version>  
</dependency>

Spring Boot Starter Data JPA

This provides the spring-boot-starter-data-JPA dependency to affix a Spring application to a relational database productively. It  uses the dependency of the spring-boot-JPA internally (Spring Boot version 1.5.3 or later).

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

The database is built with tables / relations. The previous approach (JDBC) included writing SQL queries. JPA stores object data in a table and vice versa. However, JPA has evolved as a result of different thinking processes.
Prior to JPA, ORM was a more general term for these frameworks. That is why, Hibernate is known as the ORM framework.
JPA gives authorization to plot application classes to tables in the database.

  • Entity Manager: Once you have defined the plotting, it holds up all the relations with the database.
  • JPQL (Java Persistence Query Language): Provides a way to create queries that perform an entity search which is different from the SQL queries. JPQL queries recognize the mapping defined between the entities even before. You can add terms as needed.
  • Criteria API: Defines a Java-based API for performing searches in the database.

JPA vs. Hibernate

They both are co-related to each other as, hibernate is a JPA implementation. Although it is the most used ORM framework, JPA is a description that describes an API. Hibernate acknowledges the mapping, it adds between objects and tables. This will retrieve/store data from the database based on the mapping. Additional features are also provided in addition to JPA.