Spring data JPA and its benefit

Spring Data JPA is one of Spring Data modules which provides different API to perform CRUD operation. Let’s see some points which will help to understand Spring data JPA and its benefit.

Some important Pre Defined interfaces and classes of Spring Data JPA.

Spring data JPA and its benefit

The Repository is a core interface, which is a marker interface.

public interface Repository<T, ID extends Serializable> {


The CrudRepository interface extends Repository interface, contains the following method.

    • save(S entity) – Used to save a single entity at a time.
    • Iterable<S> save(Iterable<S> entities) – we can save multiple entities at a time.
    • findOne(ID id) – use to get one entity basis of id.
    • boolean exists(ID id) – used to check whether an entity is already exited in DB for given Id.
    • Iterable<T> findAll() – find all entity of  paricular type.
    • Iterable<T> findAll(Iterable<ID> ids)  – return all entity of given ids.
    • long count() – returns the number of entities
    • void delete(ID id) – delete the entity on basis of id
    • void delete(T entity) – delete the entity which one we are passing.
    • void delete(Iterable<? extends T> entities) – delete multiple entities which we are passing.
    • void deleteAll() – delete all entities.


PagingAndSortingRepository extends CrudRepository, contains the following methods.

    • Iterable<T> findAll(Sort sort) – Returns all entities sorted by the given options.
    • Page<T> findAll(Pageable pageable) – Returns a Page of entities basis of paging restriction provided in the object.


JpaRepository extends PagingAndSortingRepository interface, contains following methods.

    • List<T> findAll().
    • List<T> findAll(Sort sort).
    • List<T> findAll(Iterable<ID> ids).
    • <S extends T> List<S> save(Iterable<S> entities).
    • void flush().
    • <S extends T> S saveAndFlush(S entity).
    • void deleteInBatch(Iterable<T> entities).
    • void deleteAllInBatch().
    • T getOne(ID id).


We have seen important interfaces and its method. Let’s see what are the benefits of Spring Data JPA.

Benefits of Spring Data JPA – The Spring Data JPA is mainly used to the development of the repository layer. In Spring Data we can define the repository interfaces and methods to retrieve the data.

Consider we have an entity called Student.java and we have some records in the database as below.


package com.javatute.entity;
public class Student {
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;
	@Column(name = "name")
	private String name;
	@Column(name = "roll_number")
	private String rollNumber;
	@Column(name = "university")
	String university;

No need to write a query, we can define a custom method to retrieve the data. For example, suppose we have a table book and entity Student.java. we have a field name in the entity and we want to get data on the basis of name. we can define a method in StudentRepository something like below –

public interface StudentRepository extends JpaRepository {
public List<Students> findByName();

Since CrudRepository provides predefined methods and we can define query methods(for accessing data) which reduce code size and Using Spring Data Jpa we can remove a lot of code by creating an abstract repository class that provides CRUD operations for our entities.

We can complex query using @Query annotation in Spring Data JPA. See an example here.

We can also write JPQL and Native Query with entity using @NamedQuery and @NamedNativeQuery annotation. See an Example here.

Spring Data Jpa provides PagingAndSortingRepository interface for sorting and pagination support. See an Example here.

Spring Data Jpa provides Named Parameters interface. See an Example here.

That’s all about Spring data JPA and its benefit.

You may like.

Other Spring Data JPA and Hibernate post.

Spring Data JPA Docs.