Java · OOP, JVM and backend craft

How do JPA and Hibernate work together?

HardjpahibernateormUpdated Oct 2026
Quiz this track

Short answer

JPA is the persistence specification and Hibernate is a common implementation. Hibernate maps entities, tracks changes in a persistence context and translates JPQL into SQL.

JPA defines annotations and interfaces such as EntityManager and EntityTransaction. Hibernate implements them and adds features such as caching and criteria queries. A persistence context is a first-level cache that holds managed entities for the duration of a transaction.

java
@Entity
public class Order {
    @Id
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    private Customer customer;

    @OneToMany(mappedBy = "order", fetch = FetchType.LAZY)
    private List<OrderLine> lines = new ArrayList<>();
}

@Query("select o from Order o join fetch o.customer where o.id = :id")
Optional<Order> findWithCustomer(long id);
A fetch join loads the customer in the same query instead of issuing one query per order.

Behaviour to understand

  • Dirty checking flushes changed managed entities at transaction commit
  • Lazy associations load on first access and throw LazyInitializationException outside a session
  • The N+1 problem issues one query for a list and one per element for a lazy association
  • JPQL operates on entities and fields, not table and column names
  • Detached entities are no longer tracked and must be merged or reloaded

Keep entities inside the persistence layer and map them to DTOs at the boundary. That avoids leaking lazy state into serialization and keeps the API contract stable. Use projections for read-heavy endpoints instead of loading full graphs.

Do not turn every association eager to fix lazy loading errors. That trades one visible query for hidden cartesian products and memory growth. Fix the query or the transaction boundary instead.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1What is the persistence context?
  2. 2How do you detect and fix an N+1 query?
  3. 3What is the difference between lazy and eager loading?
  4. 4Why should you avoid exposing entities from controllers?

Say it in the interview

JPA is the specification and Hibernate is the implementation. Hibernate manages entities in a persistence context, tracks dirty changes and translates JPQL to SQL. The two production issues I watch for are lazy loading outside a transaction and the N+1 query problem. I fix them with fetch joins or projections, keep entities in the persistence layer, and map to DTOs at the boundary.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Java mock interview and get scored feedback on depth, clarity and confidence.