Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JpaRepository vs CRUDRepository findAll

Tags:

I have a simple question: why JpaRepository is returning List of entities but CrudRepository returns Iterable of entities?

Is it done on purpose? I guess it's because CrudRepository is more generic interface and there may be some specific repository which returns Iterable.

It makes harder to use CrudRepository without using specific JpaRepository..

Thanks

like image 280
Vadim Kirilchuk Avatar asked Aug 07 '15 15:08

Vadim Kirilchuk


People also ask

Should I use JpaRepository or CrudRepository?

Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.

What is the return data type of findAll method in CrudRepository?

The CrudRepository interface declares many methods, but the methods that are relevant for this blog post are described in the following: The void delete(T entity) method deletes the entity whose id is given as a method parameter. The Iterable<T> findAll() method returns all entities that are saved to the database.

Why do we use JpaRepository?

JpaRepository is particularly a JPA specific extension for Repository. It has full API CrudRepository and PagingAndSortingRepository. So, basically, Jpa Repository contains the APIs for basic CRUD operations, the APIS for pagination, and the APIs for sorting.

What is CrudRepository?

CrudRepository is a Spring Data interface for generic CRUD operations on a repository of a specific type. It provides several methods out of the box for interacting with a database.


1 Answers

The class CrudRepository is part of the Spring Data Commons project and is the recommended interface to extend regardless of the actual data store used.

The reason CrudRepository methods return Iterable and not List (or Set) is because some data stores allow streaming of results and using a Collection type would result in loss of functionality for such stores.

like image 153
manish Avatar answered Sep 28 '22 12:09

manish