skip to content

Spring Data JPA

The JPA-flavoured repository module: JpaRepository, derived queries, @Query with JPQL or native SQL, entity graphs, locking, Specifications and stored procedures. Most Java backend interviews spend real time here, because most Java backends persist through it.

part ofSpring Frameworkoverview, primer and where to startread it →
on this pageshow

explore

questions

page 2 of 2

Design a memory-safe path to export millions of rows via a Spring Data JPA repository. Which mechanisms do you combine and why?

level: principalimportance: should knowfreq 25%

basics

~10 s

Return a Stream<T>, consume it inside a read-only transaction, and close it with try-with-resources. Add @QueryHints for fetch size and readOnly, and periodically detach/clear entities so the persistence context and heap stay bounded.

open as a page

How does the SpEL expression #{#entityName} work in a @Query, and why is it useful?

level: principalimportance: should knowfreq 30%

basics

~20 s

Spring Data lets you embed SpEL in a @Query using #{...}. The built-in #entityName variable resolves to the managed entity's name at runtime, so you can write generic base-repository queries like SELECT e FROM #{#entityName} e that work for any entity subtype.

open as a page

When should you extend JpaRepository versus compose custom repository fragments?

level: principalimportance: should knowfreq 40%

basics

~20 s

Extend JpaRepository for standard CRUD, derived queries and @Query methods. Add a custom fragment (an interface plus an Impl) when a method needs hand-written logic — EntityManager, Criteria API, QueryDSL — that derived queries and @Query can't express. Compose narrow fragments to restrict the exposed API.

open as a page

When would you choose Specifications over QueryDSL, Query-by-Example, or @Query, and what are their trade-offs at scale?

level: principalimportance: should knowfreq 30%

basics

~20 s

Use Specifications for dynamic, reusable, composable filters built at runtime with type-safe Criteria. Prefer @Query for fixed complex queries, Query-by-Example for simple probe matching, and QueryDSL when you want a fluent DSL and heavy compile-time safety.

open as a page

How do you map a stored procedure that returns a result set (REF_CURSOR) or multiple OUT parameters in Spring Data JPA?

level: seniorimportance: nice to knowfreq 20%

basics

~10 s

Declare a JPA @NamedStoredProcedureQuery on an entity with @StoredProcedureParameter entries — use mode REF_CURSOR for a returned result set or several OUT parameters — then reference it from the repository via @Procedure(name = "...").

open as a page

showing 31–35 of 35