Spring Data JPA

It is one of the projects from the spring.io/projects. It reduces writing a lot of the boilerplate code.

What does it do?

  1. Creates a DAO (Data Access object). DAO is a design pattern.
    A DAO is an object that provides an abstract interface to some type of database. By mapping application calls to the persistence layer (a group of files that are used to communicate between the application and the Database), DAOs provide some specific data operations without exposing details of the database.
    This isolation supports the Single responsibility principle. It separates what data accesses the application needs, in terms of domain-specific objects and data types (the public interface of the DAO), and how these needs can be satisfied with a specific DBMS, database schema, etc. (the implementation of the DAO).
  2. From that, Spring will give you CRUD implementation for free hence reduces the DAO code to write.
The entity can be replaced with any other entity and it will work the same. This eliminates the creation of a new DAO every time a new a entity needs to be used

JpaRepository

Development Process

  1. Extend jpaRepository
  2. Use your Repository in your application
  3. Now you have access to all the methods form the jpaRepository

import org.springframework.data.jpa.repository.JpaRepository;

public interface PersonRepository extends JpaRepository<PersonModel,Long>{

//no need to write anymore code and all the methods are inherited


}

References

https://www.kelltontech.com/kellton-tech-blog/spring-and-data-access-object-dao-part-1
Check out the full list of available at the java doc for JpaRepository
Udemy: Spring 5 by Chad Derby: https://www.udemy.com/share/1000qYBUcTdFlVRno=/

Leave a comment

Design a site like this with WordPress.com
Get started