Java JPA and Hibernate

The abstraction that hides SQL until it hurts. Most production problems here are the ORM doing exactly what you told it to.

4 concepts · 11 interview questions

What this topic covers

Every concept in jpa and hibernate, and the questions each one gets asked as. Where a question links, it has a full write-up.

Entities and mappings

A class mapped to a table, with relationships expressed as object references and an owning side that decides what is written.

  • What are the requirements for a JPA entity class?
  • Which side owns a bidirectional relationship, and why does it matter?
  • What are the fetch types, and which is the default for each mapping?

The persistence context

A first-level cache and unit of work: managed entities are tracked, and changes are flushed automatically at commit.

  • What are the entity lifecycle states?
  • What is dirty checking, and why did my entity save without a save() call?
  • save() versus persist() versus merge()?

ORM performance

N+1 queries, over-fetching, and caches that hide correctness problems — all invisible until you count the queries.

  • What is the N+1 problem, and how do you detect it?
  • JOIN FETCH, entity graph, or batch size — which and when?
  • When is the second-level cache a mistake?

Where the ORM should stop

Entities are a persistence concern. Letting them travel to the API boundary couples your contract to your schema.

  • Should you return a JPA entity from a REST controller?
  • When should you drop to native SQL?

More in Databases and persistence

See the whole reference map →