Skip to content
Preptima

Top 5 Hibernate Interview Questions

Browse 5 real-world technical and behavioral interview questions about Hibernate. Review scenarios, edge cases, and architectural best practices.

Your lazy-loading proxy works until someone calls equals or getClass on it. What breaks, and how do you design a proxy that survives it?

A lazy-loading proxy is a different object of a different class from its subject, so getClass comparisons, direct field reads and identity checks all see the proxy. A proxy pattern that survives defines equality by an identity key both sides answer without loading, compared through instanceof and accessors.

harddesign

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.

hardscenario

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.

mediumconcept

This endpoint logs one query per row. Why are you seeing N+1 queries, and how do you fix them?

You get N+1 when one query loads N rows and something later dereferences a lazy association on each of them, firing a select apiece. Fix it per call site with join fetch, an entity graph, or batch fetching - not by changing mappings to EAGER.

mediumconcept

You pass a thousand new entities to saveAll and the database sees a thousand separate inserts. How do you get them batched?

Spring Data JPA saveAll batching needs a Hibernate batch size, statements for the same table kept adjacent, and an identifier strategy that does not force a round trip per row. IDENTITY generation silently prevents insert batching, because Hibernate has to execute each insert to learn the key.

hardconcept