Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 28

    Hibernate Basics

    Hibernate is the default JPA provider Spring Boot ships with.

    Course progress0%
    Focus
    3 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    Hibernate is the default JPA provider Spring Boot ships with. It handles the entity ↔ row mapping, lazy loading, dirty checking and 1L caching for you. Knowing how it works prevents 90% of mysterious N+1 bugs.

    Understanding the topic

    The mental model:

    • Persistence Context — a per-transaction map of managed entities.
    • Dirty checking — Hibernate compares snapshots and auto-issues UPDATE on commit.
    • Lazy loading — relations fetched only when accessed (requires open session).
    • First-level cache — same entity within a TX = same Java instance.

    Best practices

    • Always use FetchType.LAZY on @OneToMany/@ManyToMany — defaults bite.
    • Avoid @Transactional(readOnly=false) for queries — set readOnly=true for read endpoints.
    • Disable open-in-view in production: spring.jpa.open-in-view: false.
    Ready to mark this lesson complete?Track your journey across the entire course.