Loading…
JPA, Hibernate, transactions, N+1, SQL tuning, indexes
The data layer is where careers are made and lost in a Java interview. This hub pairs Spring Data JPA and Hibernate (entity mapping, fetch strategies, the N+1 problem, transactions and isolation levels) with the underlying SQL and relational fundamentals (indexes, query plans, joins, ACID) so you can answer any data-access question — ORM-side or SQL-side — with confidence.
How to use this hub
2 modules cover hibernate, jpa & database end-to-end. Open the first module, use Open question 1 on its landing page, and use Next at the bottom of each answer to stay in order.
Revision sheet
~12 min · 5 sections · save as PDF
Database persistence is the backbone of every business application, and interviewers use JPA/Hibernate questions to test whether you understand what is happening beneath Spring Data's magic repository methods. The N+1 problem, LazyInitializationException, and @Transactional misuse are the three most cited production bugs in Java applications — freshers who understand them stand out immediately. Knowing the difference between JPA (specification) and Hibernate (implementation) also signals architecture awareness.
mappedBy side is NOT the owner; the owning side holds the foreign key columnjpa-basics-and-orm-concepts — drill: ORM benefit, entity states and what triggers each transition, EntityManager vs Spring Data repositoryentity-mapping-annotations — drill: @Entity + @Id is minimum, @GeneratedValue strategies (IDENTITY is most common for MySQL/PostgreSQL), @Column nullable/uniquejpa-relationships — drill: @OneToMany with mappedBy on the non-owner side; @ManyToOne on the child is the owning side; cascade ALL vs specific typesspring-data-repositories — drill: JpaRepository extends PagingAndSortingRepository extends CrudRepository; derived query naming rulesjpql-and-derived-queries — drill: JPQL uses entity/field names not table/column names; Pageable for paginationtransactions-basics — drill: @Transactional default propagation = REQUIRED; checked exceptions do NOT trigger rollback by defaultlazy-vs-eager-loading — drill: @OneToMany default = LAZY; @ManyToOne default = EAGER; LazyInitializationException = access after tx closedcomparisons — drill: JPA vs Hibernate, JPQL vs SQL, LAZY vs EAGER with N+1 implicationsscenario-based — drill: fix LazyInitializationException (open session in view or @EntityGraph or DTO projection), N+1 fix"In my Spring Boot projects I use Spring Data JPA with Hibernate as the JPA provider. I annotate entity classes with @Entity and @Id, map relationships with @OneToMany and @ManyToOne, and write queries using derived method names or @Query JPQL. I'm careful about LAZY vs EAGER loading — I keep collections LAZY and use @EntityGraph or JOIN FETCH when I need the related data in a specific query to avoid the N+1 problem. I annotate service methods with @Transactional to ensure that operations run atomically and roll back on failure."
Revision sheet
~12 min · 5 sections · save as PDF
SQL interview questions are unavoidable in any backend interview — joins (inner, outer, cross, self), subqueries vs joins, window functions, indexes (B-tree vs hash, when an index hurts), the EXPLAIN plan, transaction isolation levels, ACID, normalization, sharding, partitioning, replication, connection pooling, and migration tools (Flyway, Liquibase). This page covers cross-database SQL fundamentals plus the operational questions that decide a senior interview. For Postgres-specific deep dives (window functions, JSONB, MVCC, partitioning), use the dedicated PostgreSQL page; for MongoDB, see the MongoDB page; for Redis, see the Redis page.
Three lenses senior interviews probe:
EXPLAIN is your evidence. Always read the plan, not the SQL.
The interview surface for this module breaks into these focus areas:
WHERE func(col), leading wildcard LIKE '%x', mismatched types, low selectivity.connections × instances against DB max."I'd write the query, run `EXPLAIN ANALYZE`, look for
on the hot path, fix it with , and only after the query is tight do I think about ."