Section 16 · Stage 4: Build Applications
Spring Data JPA & Hibernate
Map Java objects to tables, model relationships, write repository queries and avoid the common performance traps.
What you will be able to do
- Explain what ORM, JPA and Hibernate are and when to use them
- Map classes to tables, including one-to-many and many-to-many relationships
- Write repository queries with derived methods, JPQL and native SQL
- Use pagination, sorting and transactions in a Spring application
- Find and fix the N+1 problem and other common JPA performance traps
- Manage schema changes with Flyway migrations
What you will learn
16.1ORM, JPA & Hibernate6 lessons
Why we map objects to tables, how JPA, Hibernate and Spring Data JPA relate, and how to add them to a Spring Boot 4 app on MySQL.
16.2Entities & Mapping8 lessons
Turn Java classes into database tables with annotations: ids, columns, enums, money, embedded value objects, auditing and correct equals/hashCode.
- @Entity, @Table, @Id and @GeneratedValue40 min
- @Column: names, lengths, nullability and uniqueness45 min
- Enums, dates and BigDecimal fields50 min
- Embedded value objects with @Embeddable45 min
- Auditing: created and updated timestamps45 min
- equals and hashCode for entities45 min
- Practice: entities and mapping55 min
- ShopLite: map products, customers, addresses and orders60 min
16.3Repositories & Queries8 lessons
Read and write data with Spring Data repositories: built-in CRUD, derived methods, JPQL, native SQL, projections and bulk updates.
- JpaRepository and the built-in CRUD methods45 min
- Derived query methods from method names45 min
- @Query with JPQL45 min
- Native SQL queries40 min
- Projections and DTO queries45 min
- @Modifying update and delete queries45 min
- Practice: repositories and queries50 min
- ShopLite: product and customer repositories on Spring Data JPA60 min
16.4Relationships8 lessons
Map one-to-one, one-to-many and many-to-many links between entities, keep both sides in sync and use cascades safely.
16.5The Persistence Context & Entity Lifecycle7 lessons
Understand how Hibernate tracks objects, when it sends SQL and why lazy loading fails, so its behaviour stops being a surprise.
- Transient, managed, detached and removed40 min
- Dirty checking: updates without save()45 min
- Flush: when SQL is actually sent45 min
- save vs saveAndFlush, persist vs merge45 min
- LazyInitializationException and why it happens45 min
- Practice: persistence context and lifecycle55 min
- ShopLite: updates with dirty checking60 min
16.6Transactions in Spring8 lessons
Use @Transactional correctly so business operations are all-or-nothing, and protect data from concurrent updates with locks.
- @Transactional on service methods45 min
- Rollback rules for checked and unchecked exceptions45 min
- Read-only transactions45 min
- Propagation basics: REQUIRED and REQUIRES_NEW45 min
- Self-invocation and other reasons @Transactional is ignored45 min
- Optimistic locking with @Version vs pessimistic locks55 min
- Practice: transactions in Spring50 min
- ShopLite: cancel an order safely60 min
16.7Pagination, Sorting & Searching7 lessons
Return large result sets in pages, sort them safely and support optional filters in REST endpoints.
16.8Performance Traps8 lessons
Spot and fix the problems that make JPA applications slow: eager fetching, N+1 queries, entities in the web layer and Open Session in View.
- Lazy vs eager fetching40 min
- The N+1 query problem45 min
- Fixes: JOIN FETCH, @EntityGraph and batch fetching55 min
- Returning DTOs instead of entities45 min
- Open Session in View and why to turn it off45 min
- Counting queries in tests and logs50 min
- Practice: JPA performance traps50 min
- ShopLite: a fast product listing75 min
16.9Schema Migrations with Flyway7 lessons
Version your database schema like code with Flyway instead of letting Hibernate change it.
16.10JPA Mini Project: ShopLite on JPA5 lessons
Finish moving ShopLite from JdbcTemplate to Spring Data JPA with relationships, paged search, safe order placement, migrations and no N+1.
