Spring JPA Introduction


Spring JPA Introduction

Spring Data JPA is a data provider, not a JPA provider, and it's a library/framework that sits on top of our JPA provider and offers an extra layer of abstraction. If we choose to use Spring Data JPA, our application's repository layer will consist of three layers, as shown below:

By extending the Spring Data repository interfaces, Spring Data JPA supports establishing JPA repositories.

Spring Data Commons provides the infrastructure shared by the datastore-specific Spring Data projects. The JPA provider implements the Java Persistence API.

Spring Data JPA appears to make our program more complicated at first glance, and in some ways, this is correct. It adds another layer to our repository, but it also saves us from writing any boilerplate code.

Spring JpaTemplate's Benefits

You don't have to write the before and after code for persisting, updating, deleting, or searching an object, such as creating Persistence instance, EntityTransaction instance, EntityManagerFactory instance, committing EntityTransaction instance, EntityManager instance, and closing EntityManager instance.

As a result, it saves a lot of code.

Configuring a Spring Application's Persistence Layer

By following these steps, we can create the configuration class that configures a Spring application's persistence layer:

  • Create a properties file with our application context configuration class needs.
  • Create a data source bean and configure it.
  • Configure the factory bean for the entity manager.
  • Set the transaction manager bean's properties.
  • Annotation-driven transaction management is enabled.
  • Set up Spring Data JPA.