Spring Data JPA Background



When we develop a Java-based enterprise application, we isolate the code across the classes and interfaces in the form of layers. These layers are Data access layers, Service Layers, Presentation layers, and sometimes integration layers. The data access layers are responsible for connecting to the database and execute all the SQL statements that our application needs against the database. It pushes and fetches data to and from the databases and hands over the data to service layers where all the business logic gets executed. Finally Presentation layers use the service layers and present the result to the end-user. Sometimes integration layers allows to integrate with our application or sometimes our application gets connected with other application to consume services provided by that application and this happen through the integration layer. Since Spring Data JPA works with the Data access layer, so our detailed discussion will revolve around this layer.

What is ORM

ORM stands for object−relational mapping. It is a process of mapping a Java class with a database table and fields of classes gets mapped with the columns of the tables. Once mapping is done then synchronization takes place from objects to database rows. ORM deals with these objects instead of writing SQL and invoke methods like save(), update(), delete() on the object. Invoking these methods generates SQL statements automatically and using JDBC internally ORM tool does the insertion, updation and deletion of the record to/from database.

What is JPA

JPA is a standard from Oracle that stands for Java persistence API, to perform object−relational mapping in Java−based enterprise application. Like any other standard from Oracle JPA also comes with some specifications and some APIs. Specification is nothing but a set of rules written in plain English. Specification is for JPA vendors and the providers whereas API is for developers or programmers. Some of the popular JPA providers are HIbernate, OpenJPA, Eclipse Link etc which implements the JPA API by following the specifications. Hibernate is the most popular JPA provider used in most of the Java-based enterprise applications. A JPA API is a set of classes such as EntityManagerFactory, EntityManager, etc. The annotations like @Entity, @Table, @Id, and @Column is the most widely used annotation from JPA.

For more details on JPA read our complete tutorials by clicking here

The problem

While working on Data access layers lets say for User module typically we create UserDao which is an interface and then we will create a class UserDaoImpl which will implement that interface, and this will, in turn, use the EntityManager and EntityManagerFactory from JPA API, or it may use the hibernate template from the Spring. These classes, in turn, depend on the Datasource which usually gets configured as the implementation of Datasource i.e.,DriverManagerDataSource from Spring which knows how to connect to the data source and this whole process gets repeated almost for every module or entity that we develop in our application. Apart from these, there were some more challenges(configuration relate, performing pagination and auditing related) that a developer have to deal manually with every application. This overall process was sometimes error-prone and time−consuming.

The Solution

This is the place where Spring data JPA comes in and says Dont perform these stuff and avoid these boilerplate coding and configuration instead this will be performed internally by myself(Spring Data JPA). what a developer need s to do is to come up with Data access layers and define domain objects or JPA entities such as User and all the fields associated with it and then define an interface that will be your repository including custom finder methods and then Spring will provide the implementation automatically.

Advertisements