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
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With