# Revision and Interview Questions — Hibernate / JPA

Source: https://www.skillbyai.com/en/hibernate-jpa/p-revision

> Recall JPA and Hibernate concepts quickly for exams and interviews.

## Cheat sheet

**Basics**: ORM bridges objects and tables; JPA (jakarta.persistence) is the spec, Hibernate the implementation, Spring Data JPA generates repositories; ddl-auto=validate plus Flyway/Liquibase. **Entities**: @Entity, @Id, no-arg constructor, @Table/@Column, BigDecimal for money, @Enumerated(STRING), embeddables, converters, JSON via @JdbcTypeCode. **IDs**: SEQUENCE with allocationSize (batch-friendly), IDENTITY (no insert batching), UUID, @NaturalId, @EmbeddedId. **Inheritance**: @MappedSuperclass, SINGLE_TABLE, JOINED, TABLE_PER_CLASS. **Persistence context**: transient, managed, detached, removed; persist, find, getReference, merge (returns managed copy), remove; dirty checking; first-level cache; flush before commit and relevant queries. **Relationships**: @ManyToOne owns the FK; @OneToMany(mappedBy) inverse; helper methods; Set for @ManyToMany; explicit association entity when the link has data; @MapsId one-to-one; cascade and orphanRemoval only for owned children; careful equals/hashCode. **Fetching**: @ManyToOne/@OneToOne EAGER by default, make LAZY; LazyInitializationException; open-in-view off; N+1 fixed by join fetch, entity graphs, batch fetching, DTO projections. **Queries**: JPQL with parameters, keyset pagination, Criteria/Specifications for dynamic filters, native SQL, bulk updates bypass the context. **Transactions**: @Transactional at service layer, rollback on unchecked by default, propagation, self-invocation pitfall; @Version optimistic locking; pessimistic FOR UPDATE; L2 cache for reference data.

## Common interview questions

Answer each with an example and a trade-off.

```text
1. What is the difference between JPA, Hibernate and Spring Data JPA?
2. Explain the four entity states and automatic dirty checking.
3. persist vs merge: what does merge return?
4. What is the owning side of a relationship and what does mappedBy do?
5. Why should @ManyToOne be made LAZY, and what is LazyInitializationException?
6. What is the N+1 select problem and how do you fix it?
7. SEQUENCE vs IDENTITY ID generation: which allows batching and why?
8. Compare SINGLE_TABLE and JOINED inheritance strategies.
9. How does @Version prevent lost updates? When would you use a pessimistic lock?
10. Why does @Transactional not work on a method called from the same class?
11. When would you use DTO projections instead of entities?
12. What are the risks of JPQL bulk updates?
```

## Talk in SQL terms

Strong answers explain what SQL Hibernate generates: "join fetch turns 1 + N selects into one join", "@Version adds AND version = ? to the UPDATE". It shows you understand the ORM rather than treating it as magic.

**Quiz:** Which combination fixes an N+1 problem when listing orders with their customer names?

- [ ] Make customer EAGER and enable open-in-view
- [ ] Increase the transaction timeout
- [ ] Use EnumType.ORDINAL
- [x] Use join fetch or an entity graph for customer, or a DTO projection selecting the name

*Answer:* Use join fetch or an entity graph for customer, or a DTO projection selecting the name. Fetching the association in the same query or projecting the needed column avoids per-row queries.
