Skip to content
Preptima

Spring Data & JPA interview questions

Entity lifecycle, lazy loading and N+1, transaction propagation, and generated-SQL debugging.

4 questions in Java & Spring.

medium2

mediumConceptScenario

Nobody called save, but the UPDATE still went to the database. Explain how that happened.

Hibernate dirty checking tracks a managed JPA entity through the persistence context. At flush the provider compares the entity against its loaded snapshot and writes an UPDATE for whatever differs, so mutating a managed object inside a transaction is itself the instruction to persist. Use this spring data jpa answer to show the decision, trade-off, and evidence rather than a memorised definition.

5 minmid, senior, staff

hard2

hardScenarioConcept

A Spring Data repository method looks like one clean line, but the endpoint issues several hundred queries per request. How do you diagnose the N+1, and what would you actually change?

The repository call fetches parents in one query; the extra queries come later, when something touches a lazy association - usually the JSON serialiser, with open-session-in-view keeping the context alive so it succeeds silently. Diagnose by counting statements per request, then fix with a join fetch or entity graph, batch fetching, or a DTO projection.

4 minmid, senior, staff