skip to content

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 pageshow

explore

questions

156 · 6 sections

What is Query by Example (QBE) in Spring Data, and what are the three building blocks?

level: juniorimportance: must knowfreq 55%
basics
~20 s

Query 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).

open as a page

What is a custom fragment interface in Spring Data, and how does Spring find its implementation?

level: juniorimportance: must knowfreq 55%
basics
~10 s

A 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'.

open as a page

When you call repository.save(entity), how does Spring Data decide whether to run an INSERT or an UPDATE by default?

level: juniorimportance: must knowfreq 62%
basics
~10 s

By 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.

open as a page

You declare a Spring Data repository as just an interface with no body. Who provides the actual implementation, and when?

level: juniorimportance: must knowfreq 72%
basics
~20 s

You 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.

open as a page

What are derived query methods in Spring Data, and how does a method name like findByLastName become a query?

level: juniorimportance: must knowfreq 72%
basics
~10 s

You 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.

open as a page

What are the four JPA auditing annotations in Spring Data, and what two things must you configure to make them populate automatically?

level: juniorimportance: must knowfreq 70%
basics
~10 s

The 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.

open as a page

What is a class-based DTO projection in Spring Data, and why use one instead of returning the entity?

level: juniorimportance: must knowfreq 70%
basics
~20 s

A 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.

open as a page

What is an interface-based projection in Spring Data, and why would you use one instead of returning the full entity?

level: juniorimportance: must knowfreq 72%
basics
~20 s

An 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.

open as a page

What is the difference between returning a Page<T> and a Slice<T> from a Spring Data repository method?

level: juniorimportance: must knowfreq 70%
basics
~20 s

A 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.

open as a page

What is Pageable in Spring Data, and how do you create and use a PageRequest to fetch one page of results?

level: juniorimportance: must knowfreq 72%
basics
~20 s

Pageable 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.

open as a page

What are JPA derived query methods, and how do keywords like IgnoreCase and OrderBy shape the generated query?

level: juniorimportance: must knowfreq 75%
basics
~10 s

Derived 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.

open as a page

What is @EntityGraph in Spring Data JPA, and what problem does it solve?

level: juniorimportance: must knowfreq 70%
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.

open as a page

What is optimistic locking in JPA, and how does the @Version field implement it?

level: juniorimportance: must knowfreq 60%
basics
~10 s

Optimistic 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.

open as a page

What is the @Query annotation in Spring Data JPA, and how do you bind parameters to a JPQL query?

level: juniorimportance: must knowfreq 78%
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.

open as a page

What is JpaRepository and where does it sit in the Spring Data repository interface hierarchy?

level: juniorimportance: must knowfreq 78%
basics
~10 s

JpaRepository 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.

open as a page

In Spring Data JDBC, what is an aggregate and an aggregate root, and how are they persisted?

level: juniorimportance: must knowfreq 60%
basics
~20 s

An 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.

open as a page

In Spring Data JDBC, what language does the @Query annotation expect, and how does that differ from Spring Data JPA?

level: juniorimportance: must knowfreq 70%
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.

open as a page

Why does Spring Data R2DBC not support entity relationships or lazy loading, and what does that mean for how you model data?

level: juniorimportance: must knowfreq 70%
basics
~10 s

R2DBC 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.

open as a page

What is an R2dbcRepository, and what do its query methods return?

level: juniorimportance: must knowfreq 72%
basics
~10 s

R2dbcRepository 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.

open as a page

What is AggregateReference in Spring Data JDBC, why does it exist, and how does it behave on read and write?

level: middleimportance: must knowfreq 40%
basics
~20 s

AggregateReference<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.

open as a page

What do @Document, @Id, and @Field do when mapping a Java/Kotlin class to a MongoDB collection with Spring Data MongoDB?

level: juniorimportance: must knowfreq 70%
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.

open as a page

What is ReactiveMongoRepository and what do its query methods return?

level: juniorimportance: must knowfreq 70%
basics
~10 s

It 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.

open as a page

What is a MongoRepository and how do derived (query-method) queries work in Spring Data MongoDB?

level: juniorimportance: must knowfreq 75%
basics
~20 s

MongoRepository 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.

open as a page

How do you query documents with MongoTemplate using Query and Criteria?

level: juniorimportance: must knowfreq 70%
basics
~10 s

Build 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.

open as a page

How does Redis back the Spring Cache abstraction, and what is the role of RedisCacheManager?

level: juniorimportance: must knowfreq 60%
basics
~20 s

RedisCacheManager 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.

open as a page

What is HAL, and what do the _links and _embedded properties in a Spring Data REST response mean?

level: juniorimportance: must knowfreq 62%
basics
~10 s

HAL (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.

open as a page

What does Spring Data REST do with your repositories, and what role does @RepositoryRestResource play?

level: juniorimportance: must knowfreq 55%
basics
~10 s

Spring 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.

open as a page

How do @Projection interfaces and an excerptProjection change the exposed representation in Spring Data REST?

level: middleimportance: must knowfreq 55%
basics
~10 s

A @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).

open as a page

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?

level: middleimportance: should knowfreq 42%
basics
~20 s

exported=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.

open as a page

How do @RepositoryRestResource and @RestResource change the URL path and HAL link relation of an exposed resource, and how do path and rel differ?

level: middleimportance: should knowfreq 40%
basics
~20 s

path 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.

open as a page