Spring Data JPA & Hibernate
Repositories and save() internals, derived query keywords, pagination and keyset scrolling, @Query and JPQL, native SQL, entity mapping and ID strategies, the persistence context and dirty checking, relationships and cascading, fetching and the N+1 problem, transactions, propagation and locking, projections, Specifications and auditing, inheritance and composite keys, second-level caching, HikariCP, Flyway migrations, testing with Testcontainers and advanced Hibernate features.
Introduction to Spring Data JPA
From JDBC and Hibernate to Spring Data JPA, the layers involved, setup and configuration, a first entity and repository, CRUD methods, how save() decides between persist and merge, and the repository interface hierarchy.
Repository Methods (Derived Queries)
How Spring Data parses method names into queries, prefixes like findBy, existsBy, countBy and deleteBy, Top and First, Distinct, IgnoreCase, And and Or, supported return types, and the pitfalls of derived queries.
Derived Query Keywords
Every common keyword Spring Data JPA understands in method names — comparison, Between, Before/After, Like, Containing, StartingWith, In, True/False, Null, IgnoreCase and OrderBy — combining them, nested properties and associations, and when to stop.
Pagination & Sorting
Why never to load everything, Pageable and PageRequest, Page metadata, the SQL behind a page, Sort and multi-column sorting, pageable REST endpoints, Page vs Slice, deep-page performance and keyset pagination with Spring Data's scrolling API.
@Query & JPQL
Why @Query exists, JPQL vs SQL, named and positional parameters, LIKE, aggregates, GROUP BY and HAVING, @Modifying updates and deletes and their persistence-context pitfalls, joins, JOIN FETCH, subqueries, DTO projections and common mistakes.
Native SQL Queries
What native queries are, JPQL vs native SQL and when to choose each, parameters, mapping results to entities, Object[] and interface projections, native updates, deletes and inserts, pagination with countQuery, sorting, stored procedures, vendor functions, CTEs, window functions and performance.
Entity Mapping Essentials
What makes a class an entity, @Table and @Column, primary keys and generation strategies (IDENTITY, SEQUENCE, UUID), enums with EnumType.STRING, dates, BigDecimal for money, @Transient, @Lob, embeddables, naming strategies and schema validation.
Persistence Context & Entity Lifecycle
The persistence context as a first-level cache and unit of work, the four entity states, persist, merge, remove and detach, dirty checking, flush modes and timing, getReferenceById, LazyInitializationException, Open Session in View and lifecycle callbacks.
Relationships & Cascading
@ManyToOne, @OneToMany, @OneToOne and @ManyToMany, the owning side and mappedBy, join columns and join tables, bidirectional helper methods, cascade types, orphanRemoval, default fetch types, equals/hashCode for entities and JSON recursion.
Fetching Strategies & the N+1 Problem
LAZY vs EAGER, how proxies and lazy collections work, the N+1 select problem and how to detect it, JOIN FETCH, @EntityGraph, @BatchSize and default batch fetch size, DTO projections, collection fetching with pagination, MultipleBagFetchException and JDBC batching.
Transactions & Locking
How @Transactional works through proxies, where to put it, readOnly, rollback rules for checked exceptions, propagation (REQUIRED, REQUIRES_NEW, NESTED…), isolation levels, the self-invocation trap, optimistic locking with @Version and pessimistic locks with @Lock.
Projections, Specifications & Auditing
Interface, class/record and dynamic projections, closed vs open projections, dynamic search with Specifications and JpaSpecificationExecutor, Query by Example, custom repository fragments, and auditing with @CreatedDate, @LastModifiedBy and AuditorAware.
Inheritance, Composite Keys & Collections
Mapping class hierarchies with @MappedSuperclass, SINGLE_TABLE, JOINED and TABLE_PER_CLASS, polymorphic queries, composite primary keys with @EmbeddedId and @IdClass, join entities with @MapsId, @ElementCollection and @NaturalId.
Caching, Connection Pooling & Migrations
Hibernate's second-level cache and query cache, cache concurrency strategies and when not to cache, sizing and tuning the HikariCP connection pool, leak detection, and versioned schema migrations with Flyway and Liquibase.
Testing the Data Layer
What to test in a JPA application, @DataJpaTest and TestEntityManager, why H2 can mislead you, Testcontainers with @ServiceConnection, flushing to catch constraint errors, the false confidence of transactional tests, asserting query counts to catch N+1, and test data strategies.
Advanced Hibernate Features
Soft deletes with @SoftDelete and @SQLRestriction, JSON columns, @Formula and @DynamicUpdate, change history with Hibernate Envers, domain events, StatelessSession for bulk jobs, multiple data sources, read-replica routing, multi-tenancy, Querydsl, and when R2DBC or plain JDBC is the better tool.
JPA & Hibernate Interview Cheat Sheet
The whole topic on one page — repositories, queries, mapping, the persistence context, relationships, fetching and N+1, transactions and locking, inheritance, caching, pooling, migrations and testing — plus the most-asked interview questions, scenario questions and classic traps.