Spring Boot

Spring Data Repositories

Spring Data repositories, CrudRepository vs JpaRepository, custom queries, @EntityGraph, projections

25 Interview-Fragen·
Mid-Level
1

What is the main difference between JpaRepository and CrudRepository?

Antwort

JpaRepository extends both CrudRepository and PagingAndSortingRepository, adding JPA-specific methods like flush(), saveAndFlush(), and deleteInBatch(). CrudRepository only provides basic CRUD operations (save, findById, delete). Using JpaRepository gives access to all advanced JPA features without needing to compose multiple interfaces.

2

What is the role of PagingAndSortingRepository in the repository hierarchy?

Antwort

PagingAndSortingRepository extends CrudRepository by adding findAll(Pageable) and findAll(Sort) methods for pagination and sorting. This allows retrieving data in pages with Page/Slice and sorting results. JpaRepository extends this interface, thus providing pagination, sorting, and JPA-specific features in a single interface.

3

What is the correct hierarchy of Spring Data repository interfaces?

Antwort

The correct hierarchy is Repository (base) → CrudRepository (CRUD) → PagingAndSortingRepository (pagination/sorting) → JpaRepository (JPA-specific). Each level adds features: CrudRepository adds save/delete, PagingAndSortingRepository adds pagination/sorting, and JpaRepository adds flush/batch. This architecture allows choosing the required feature level.

4

Which JpaRepository method allows immediately synchronizing changes with the database?

5

In which case prefer CrudRepository over JpaRepository?

+22 Interview-Fragen

Meistere Spring Boot für dein nächstes Interview

Zugang zu allen Fragen, Flashcards, technischen Tests, Code-Review-Übungen und Interview-Simulatoren.

Kostenlos starten