noscript

Spring Data JPA

Before learning JPA repository and CrudRepository it becomes very important to understand what is and why these repositories should be used.

Spring Data JPa is a most powerful tool to create a Spring based application using JPA (Java Persistence API) as a data access layer. The essential feature of Spring Data JPA is it provides an abstraction layer on the existing JPA provider that allows applications to communicate with different databases with different access technologies through simple and consistent APIs.

The actual benefit of Spring Data JPA is Abstraction.  Nowadays it is the requirement of the current age to create applications that can work on any platform with any database, with the same ease and comfort. Spring Data JPA provides this abstraction specific implementation details of data storage . It also allows the programmers to just put business logic code at a higher level of abstraction. The job of the programmer is just to understand the technique to  use the Spring Data Repository interface, without taking care about the implementation of these interfaces. Spring Data provides the implementation of the unpacking of these repository interfaces.

JPA Repository

JPA Repository

This is an interface used in Spring Boot applications. Here JPA stands for Java Persistence Application. With the help of this interface we can create applications that can handle crud operations as well as paginations. Because this interface consists of all the APIs for Crud Repository and Paging and Sorting Repository.

JPA belongs to Spring Data JPA. We have to include the dependency of Spring Data JPA while performing the JPA operations.

Syntax

public interface JpaRepository<T,ID> extends PagingAndSortingRepository<T,ID>, QueryByExampleExecutor<T>

Here

 T stands for the type of class (entity / model) that we are using.

ID stands for the type of the member variable that we are using as the primary key in class.

Here is the list of all the methods included in JPA repository

List<T> findAll();

List<T> findAll(Sort sort);

 List<T> findAllById(Iterable<ID> ids);

   <S extends T> List<S> saveAll(Iterable<S> entities);

void flush();

  <S extends T> S saveAndFlush(S entity);

void deleteInBatch(Iterable<T> entities);

 void deleteAllInBatch();

 T getOne(ID id);

    @Override

    <S extends T> List<S> findAll(Example<S> example);

@Override

    <S extends T> List<S> findAll(Example<S> example, Sort sort);

Crud Repository

As the name suggests with the help of this repository, one can perform all the crud operations with the database. This interface is specifically used to perform all the manipulations with the database like insertion, updation, deletion and retrieval of the records.

With the help of this repository  we get all the methods ready to use , without worrying about the implementation details.

Syntax

public interface CrudRepository<T, ID> extends Repository<T, ID>

Here

 T stands for the type of class (entity / model) that we are using.

ID stands for the type of the member variable that we are using as the primary key in class.

Here is the list of all the methods included in CrudRepository.

<S extends T> S save(S entity);

 <S extends T> Iterable<S> saveAll(Iterable<S> entities);

  Optional<T> findById(ID id);

   boolean existsById(ID id);

 Iterable<T> findAll();

Iterable<T> findAllById(Iterable<ID> ids);

 long count();

  void deleteById(ID id);

   void delete(T entity);

  void deleteAll(Iterable<? extends T> entities);

 void deleteAll();

JPA Repository Vs Crud Repository in Java

JPA Repository Vs Crud Repository in Java

 For more information & classes Call: 2048553004
                                                  Registration Link: Click Here!

Author: Jyotsna Binjwe

Software Development Trainer

IT Education Centre Placement & Training Institute

© Copyright 2024 | IT Education Centre.