The same operation you can perform in spring data JPA by writing minimal code.Loot at below example for performing crud operation using spring data JPA. As you can see the amount of code that drastically reduced by spring data JPA.You must have realized that how simple it is to write persistence tier code using spring data JPA.I will explain each piece of it so hold on yourself.
What is Spring Data JPA
Now that you have got the basic of what spring data JPA can do.Let's talk about what it is and the features that it provides.Spring data JPA is simple to use while performing operation in database.Some of the great features provided by spring data JPA is -- Enhances standard JPA with spring
- Simplifies you data access layer
- Repository generator
- Query DSL
- Auditing and paging
Prerequisite
Spring data JPA is built on top of some of existing technology as this module is solely for understanding spring data JPA but it is important if you know basic of -- Java persistence API
- Basic hibernate concept
- Object relation mapping
- Entity relationships
- Spring core and JDBC
- JDK 1.8+
- Maven 3+
- IDE of your choice
Choosing a Data Access Layer
Whenever you working for an application and wants to choose a data access layer for persistence tier, well you have variety of options to choose in.It is not really easy to decide which one to pick ,in some scenarios you may have to choose multiple data access layer for your application.A best chosen data access layer would be the hybrid of framework.Since here we are discussing about spring data JPA ,we will discuss about the best place where spring data JPA can be used. These are some of best options that you can choose for data access layer :- JDBC or Spring JDBC : It is used for simple DB's native SQL needs if your data base table architecture is not very complex.
- Spring batch : If you have massive amount of data with hight number of SQL writes like insert and update ,choosing batch related framework would be best option.
- ORM(JPA/Hibernate or Spring data JPA : If you are dealing with data graph without excessive relationships probably ORM would be best option to support.As it has multiple mapping like one to one and many to one etc.If mapping is less and need to perform basic operation in data base table entity choosing spring data JPA would be good option.
- No Sql or MongoDB : Used for topic and threaded relational data.
Project Overview
To learn spring data JPA in details, I am going to create a project and demonstrating you the working of spring data JPA with example.Before moving forward I would recommend you to download project from https://github.com/maverickvishnu/springboot-demo.In my previous post on spring boot I have created a promo-api project.I am going to extend same project to introduce spring data JPA as well.I will discuss all the concept on spring data JPA.Though it is not essential to learn spring boot as spring data JPA is an independent concept but if you are interested you can scan through my post once on spring boot. I have used embedded H2 database and flyway DB to migrate data ,you can use any data base.In this module the more focus is on persistence tier. We will write JUnit to test spring data JPA code as our main focus in on model part. To Create spring data JPA in your project first you have to include spring data JPA dependency.As we are building an application by using spring boot ,spring boot provides spring-boot-starter-data-jpa dependency for spring data JPA.Add below dependency in pom.xml-
In below picture I have defined the data model for our project-
In the project that we already downloaded has PromotionRepository configure.Go to the com.zerototech.repo package and you can see PromotionRepository interface open that up.You can see PromotionRepository is extending generic JpaRepository. Here JpaRepository is made generic by passing Promotion entity and ID. Spring data JPA provides JpaRepository with set of methods for CRUD operation.In spring data JPA for each model defined we create a repository so that we can perform set of operation on entity defined.In order to start leveraging the Spring Data programming model with JPA, a DAO interface needs to extend the JPA specific Repository interface – JpaRepository. This will enable Spring Data to find this interface and automatically create an implementation for it.I will explain in details for now let's create a test class to test whether PromotionRepository is working fine.Add below lines of code-
As you can from above code that we are able to fetch data from Promotion repository.Congrats!!! you have created a first repository using spring data JPA.I will explain in details what is happening behind the scene.
Spring Data JPA Repository
You must be wondering by now that how spring data JPA is working internally, how data is being fetched from data base without writing any fetch query.Now it's time to delve into what is happening behind the scene.Before understanding spring data JPA let understand about spring repositories.What do you understand if somebody tell you about repository as name suggests it is a kind of data storage. Technically repositories whole point is to define a contract that data access layer will implement.The contract is an interface that bind to client code that need a data access. In the above diagram you can see in a typical spring application there is a service layer and data access layer.Consider that service needs to get data from DAO.When service layer communicates with data access layer behind the scene DAO will have implementation to fulfill service request so we define DAO Interface contract.Now this interface either can be implemented using JDBC or JPA.The important point to note here the service layer is not aware of DAO implementation.It just uses the operation given by repository interface.In case of spring data JPA the DAO interface is implemented by using JPA.Repository Architectural Overview
When we talk about repository it is nothing but a storage.In a relational database repository is nothing but a table.In a typical web application there could be many tables and they may or may not be related to each other. In the above diagram shows how a typical spring application look like.It has multiple layer to talk to while serving a request. In a real world a database would be different server.If you are developing a web application and your application needs to communicate to data base server you need a data access layer.Now that you have decided to use JPA repository for data access layer so this JPA repository will have an entity which will be mapped to database table.This how simple ORM framework works where you are mapping an object with a table.In case of spring data repository an entity is mapped one to one with JPA repository.By keeping the JPA repository focus on a single entity keeps the DAO pattern limited to that specific data and data structure.In spring service will talk to couples of JPA repository to bundle up different data from multiple entity.And finally the controller will be conversing to service to respond to a request. Below are some of important points to remember about JPA repository -- JPA repository is a Java interface not a class
- Repository is mapped one to one with JPA entity
- Focuses on DAO contract
JPA Repository Feautures
The Repository interface has only CRUD operations defined, But there are others bunch of repository interface which JpaRepository interface extends that has many more methods for different functionality. You can see in above diagram that JpaRepository is extending three different interface and all these interface is defined for providing different functionality.The spring data JPA only provides JpaRepository interface other interface have come up from core spring data.- Query DSL : This feature is given by Repository interface.
- CRUD Operations : This feature is given by CrudRepository interface.It provides bunch of helper methods as well -
- count() : To counts the total number of rows present in a table.
- exists(Long id) : To find record is available for given id.
- Paging and Sorting : Paging and sorting functionalities are provided by PagingAndSortingRepository interface.
Comments
Post a Comment