Spring Data
One repository programming model over relational and NoSQL stores, plus the per-store modules for JPA, JDBC, R2DBC, MongoDB and Redis. Interviewers ask because a repository interface with no implementation is the most magical thing in a typical Spring app.
part ofSpring Frameworkoverview, primer and where to startread it →on this pageshowhide
explore
- Repository Programming Model (Commons)30 questions
- Repository Interface Hierarchy5 questions
- Repository Proxy Generation5 questions
- Query-Derivation Engine5 questions
- Custom Fragment Interfaces5 questions
- Query by Example & Querydsl5 questions
- Persistable & Is-New Detection5 questions
- Paging, Sorting, Projections & Auditing24 questions
- Pageable, Sort & Limit5 questions
- Page vs Slice & Scrolling5 questions
- Interface-Based Projections5 questions
- DTO & Dynamic Projections4 questions
- Auditing Metadata5 questions
- Spring Data JPA35 questions
- JpaRepository Abstractions5 questions
- JPA Derived Query Methods5 questions
- @Query JPQL, Native & @Modifying5 questions
- @EntityGraph Fetch Tuning5 questions
- @Lock & Optimistic/Pessimistic Locking5 questions
- Specifications & Criteria5 questions
- @Procedure, Query Hints & Streaming5 questions
- Spring Data JDBC & R2DBC25 questions
- Spring Data JDBC Aggregates5 questions
- JDBC Mapping Model5 questions
- JDBC @Query & Templates5 questions
- R2DBC Reactive Repositories5 questions
- R2DBC Constraints & Transactions5 questions
- Spring Data MongoDB & Redis31 questions
- MongoDB Document Mapping5 questions
- MongoTemplate & Aggregation5 questions
- MongoRepository5 questions
- RedisTemplate & Redis Repositories5 questions
- Redis as Cache & Pub/Sub5 questions
- Reactive MongoDB6 questions
- Spring Data REST11 questions
- HAL Representations & Excerpts5 questions
- Repository REST Exposure6 questions
questions
156 · 6 sectionsWhat is Query by Example (QBE) in Spring Data, and what are the three building blocks?
basics
~20 sQuery by Example lets you search by filling in a sample entity (a 'probe'). Spring turns the non-null fields into a WHERE clause. The three parts are: Probe (the sample entity), ExampleMatcher (matching rules), and Example (probe + matcher combined).
What is a custom fragment interface in Spring Data, and how does Spring find its implementation?
basics
~10 sA fragment is a small interface declaring custom repository methods you write yourself. Your repository extends it, and Spring finds the implementation class by name: the fragment interface name plus the suffix 'Impl'.
When you call repository.save(entity), how does Spring Data decide whether to run an INSERT or an UPDATE by default?
basics
~10 sBy default Spring Data treats an entity as new when its @Id is null (or 0 for a primitive id type). New means INSERT; otherwise it's an existing row, so UPDATE.
You declare a Spring Data repository as just an interface with no body. Who provides the actual implementation, and when?
basics
~20 sYou never write the implementation. Spring Data creates it for you automatically at application startup — it generates a proxy object that implements your repository interface and wires it into the Spring container as a bean.
What are derived query methods in Spring Data, and how does a method name like findByLastName become a query?
basics
~10 sYou declare a method on a repository interface, like findByLastName(String name), and write no body. Spring Data reads the method name and generates the query automatically from it.
What are the four JPA auditing annotations in Spring Data, and what two things must you configure to make them populate automatically?
basics
~10 sThe annotations are @CreatedDate, @LastModifiedDate, @CreatedBy, and @LastModifiedBy on entity fields. To activate them you add @EnableJpaAuditing on a config class and attach @EntityListeners(AuditingEntityListener.class) to the entity.
What is a class-based DTO projection in Spring Data, and why use one instead of returning the entity?
basics
~20 sA repository query method returns a plain data-carrier class (often a record) holding only the fields you need, instead of the full entity. Spring Data fetches only those columns, so it is smaller and faster.
What is an interface-based projection in Spring Data, and why would you use one instead of returning the full entity?
basics
~20 sAn interface-based projection is a plain Java interface with getters. You declare it as a repository method's return type; Spring Data returns proxies exposing only those getters, so you fetch just the columns you need instead of the whole entity.
What is the difference between returning a Page<T> and a Slice<T> from a Spring Data repository method?
basics
~20 sA Page knows the total number of results (total elements and total pages), so it runs an extra COUNT query. A Slice only knows whether there is a next page, so it skips the COUNT query and is cheaper.
What is Pageable in Spring Data, and how do you create and use a PageRequest to fetch one page of results?
basics
~20 sPageable describes which slice to fetch — a page number and a page size. You create one with PageRequest.of(page, size) and pass it to a repository method. Page numbers are 0-based, so page 0 is the first page.
What are JPA derived query methods, and how do keywords like IgnoreCase and OrderBy shape the generated query?
basics
~10 sDerived queries let Spring Data JPA build a SQL/JPQL query from the method NAME. E.g. findByEmailIgnoreCase compares email case-insensitively; adding OrderByNameAsc sorts results by name ascending. No @Query needed.
What is @EntityGraph in Spring Data JPA, and what problem does it solve?
basics
~20 s@EntityGraph is an annotation you put on a repository method to tell JPA which associations to load eagerly in one query. It fixes the N+1 select problem where each parent triggers an extra query for its children.
What is optimistic locking in JPA, and how does the @Version field implement it?
basics
~10 sOptimistic locking detects concurrent edits without locking rows. You add a @Version field; Hibernate checks it in the UPDATE's WHERE clause and throws an error if another transaction already changed the row.
What is the @Query annotation in Spring Data JPA, and how do you bind parameters to a JPQL query?
basics
~20 s@Query lets you write your own query on a repository method instead of relying on the method name. By default it's JPQL. You bind values with named parameters like :name using @Param, or by position with ?1.
What is JpaRepository and where does it sit in the Spring Data repository interface hierarchy?
basics
~10 sJpaRepository is the JPA-specific top interface you extend to get ready-made CRUD, paging and sorting methods for an entity, without writing any implementation — Spring generates one at runtime.
In Spring Data JDBC, what is an aggregate and an aggregate root, and how are they persisted?
basics
~20 sAn aggregate is a group of related objects saved and loaded as one unit. The aggregate root is the top object you access it through. Spring Data JDBC has one repository per root and saves the whole aggregate together.
In Spring Data JDBC, what language does the @Query annotation expect, and how does that differ from Spring Data JPA?
basics
~10 s@Query in Spring Data JDBC takes plain, database-specific SQL. Spring Data JPA's @Query defaults to JPQL (an entity-oriented query language). Spring Data JDBC has no JPQL, so you always write real SQL.
Why does Spring Data R2DBC not support entity relationships or lazy loading, and what does that mean for how you model data?
basics
~10 sR2DBC is a non-blocking (reactive) database access layer. It has no lazy loading or @OneToMany/@ManyToOne relations because lazy loading would need a hidden blocking call. You fetch related rows with explicit extra queries yourself.
What is an R2dbcRepository, and what do its query methods return?
basics
~10 sR2dbcRepository is Spring Data's reactive repository for relational databases. Its methods return reactive types: Mono<T> for zero-or-one result and Flux<T> for many, instead of blocking values or List.
What is AggregateReference in Spring Data JDBC, why does it exist, and how does it behave on read and write?
basics
~20 sAggregateReference<T, ID> is a typed link to another aggregate root by its id only. It stores the foreign-key id in a column but does NOT load the target object — reading gives you just getId(); you fetch the referenced aggregate separately if you need it.
What do @Document, @Id, and @Field do when mapping a Java/Kotlin class to a MongoDB collection with Spring Data MongoDB?
basics
~10 s@Document marks a class as a MongoDB collection (optionally naming it). @Id marks the field used as MongoDB's _id primary key. @Field maps a Java field to a differently-named document key.
What is ReactiveMongoRepository and what do its query methods return?
basics
~10 sIt is Spring Data's reactive interface for MongoDB. Query methods return Mono<T> for zero-or-one result and Flux<T> for many. Nothing runs until you subscribe.
What is a MongoRepository and how do derived (query-method) queries work in Spring Data MongoDB?
basics
~20 sMongoRepository is a Spring Data interface for a MongoDB collection giving CRUD out of the box. You add method names like findByLastName, and Spring parses the name into a query automatically — no implementation code needed.
How do you query documents with MongoTemplate using Query and Criteria?
basics
~10 sBuild a Query object with Criteria (the filter conditions), then call mongoTemplate.find(query, EntityClass.class). Criteria.where("field").is(value) builds the filter; find returns a List of mapped objects.
How does Redis back the Spring Cache abstraction, and what is the role of RedisCacheManager?
basics
~20 sRedisCacheManager is the CacheManager implementation that stores cache entries in Redis. When you use @Cacheable, Spring asks it for a RedisCache, which reads/writes values as Redis keys so cached data survives restarts and is shared across instances.
What is HAL, and what do the _links and _embedded properties in a Spring Data REST response mean?
basics
~10 sHAL (Hypertext Application Language) is the default JSON format Spring Data REST returns. _links holds URLs to related resources (like self), and _embedded holds the actual related or collection data inline.
What does Spring Data REST do with your repositories, and what role does @RepositoryRestResource play?
basics
~10 sSpring Data REST automatically turns each Spring Data repository into a REST API with hypermedia (HAL) links — CRUD endpoints appear with no controller code. @RepositoryRestResource customizes that exposure, e.g. changing the URL path.
How do @Projection interfaces and an excerptProjection change the exposed representation in Spring Data REST?
basics
~10 sA @Projection is an interface selecting/reshaping which fields a resource exposes. You request it with ?projection=name. Setting it as excerptProjection on the repository makes it the default view for items inside collections (_embedded).
What does exported=false do at the repository, query-method, and association level, and what is the gotcha when you hide a repository that backs an association?
basics
~20 sexported=false stops Spring Data REST from publishing something over HTTP while keeping it as a normal Spring bean. On a repository it hides all its endpoints; on a query method it hides that finder; on an association it drops the association resource. Gotcha: hiding a repository can break linking to its entities.
How do @RepositoryRestResource and @RestResource change the URL path and HAL link relation of an exposed resource, and how do path and rel differ?
basics
~20 spath sets the URL segment (e.g. /members); rel sets the name of the link in the HAL _links output that clients follow. @RepositoryRestResource customizes the whole collection; @RestResource customizes a single query method or association.