Java Persistence with JPA, Hibernate, and Spring Data
Persistence with JPA and Hibernate is an advanced-level course of 5 lessons (about 2 h 46 min), with 15 exercises and 15 quiz questions. Prerequisites: Spring Boot: REST API, Database Modeling. The course needs a full project environment, planned for a later phase of the platform.
- Java
- Advanced
- 5 lessons
- 2 h 46 min
- 15 exercises
- Full project environment (phase 2)
The course in brief
This course connects your Spring Boot API to a relational database with JPA and Hibernate. You will write entities and their relationships, Spring Data repositories with derived queries, JPQL, and pagination, then master transactions, the persistence context, and the N+1 problem. You will finish with Flyway migrations, optimistic locking, and @DataJpaTest tests. The runnable exercises reproduce the ORM's mechanisms in plain Java, while the hands-on tasks are carried out in your development environment.
What you will be able to do
- Map Java classes to tables with JPA annotations and control the generated schema
- Create Spring Data JPA repositories with derived queries, JPQL, projections, and pagination
- Model consistent @ManyToOne, @OneToMany, and @ManyToMany relationships, with appropriate cascading
- Delimit transactions and leverage the persistence context (dirty checking, first-level cache)
- Detect and fix the N+1 problem and LazyInitializationException
- Version the schema with Flyway, handle concurrency with @Version, and test with @DataJpaTest
Syllabus: 5 lessons
- Explain the role of an ORM, JPA, and Hibernate
- Write a JPA entity with @Entity, @Id, @GeneratedValue, @Column, and @Enumerated
- Configure the data source and understand schema generation (ddl-auto)
- 02
Spring Data JPA Repositories and Queries
- Create a repository by extending JpaRepository
- Write queries derived from method names and JPQL queries with @Query
- Paginate and sort results with Pageable and Page
- 03
Relationships Between Entities
- Map @ManyToOne, @OneToMany, and @ManyToMany relationships
- Identify the owning side of a relationship and the mappedBy attribute
- Keep a bidirectional relationship consistent with helper methods, cascade, and orphanRemoval
JPA relationshipsOwning side and mappedByCascade and orphanRemoval
- 04
Transactions, Persistence Context, and the N+1 Problem
- Delimit transactions with @Transactional in the service layer
- Understand the persistence context: first-level cache, entity states, and dirty checking
- Diagnose and fix the N+1 problem and LazyInitializationException
- 05
Flyway Migrations, Optimistic Locking, and JPA Tests
- Version the database schema with Flyway migrations
- Protect against concurrent updates with @Version (optimistic locking)
- Test repositories with @DataJpaTest
Final project
Complete Persistence for a Shop API
Take the REST API from the Spring Boot course (or the library one) and replace the in-memory repository with JPA on PostgreSQL (or H2 in development). Model Client, Produit, Commande, and LigneCommande with their relationships, manage the schema with Flyway, and add paginated product search, a customer's order history without N+1, transactional order placement (stock decrement), and optimistic locking on the product.
Deliverables
- The Maven project with entities, repositories, transactional services, and DTOs
- The Flyway migration scripts (at least three versions)
- @DataJpaTest tests for the custom queries and a version conflict test
- A screenshot or an excerpt of the SQL logs showing that the order history runs in one or two queries
Assessment criteria
- Relationships are correctly mapped (owning side, LAZY, cascade limited to composition)
- No entity is exposed as JSON; DTOs are built inside transactions
- Order placement is atomic: if stock is insufficient, nothing is saved
- The schema is entirely managed by Flyway with ddl-auto=validate
- The N+1 problem is absent from list endpoints and the tests pass
Frequently asked questions
What is the difference between JPA, Hibernate, and Spring Data JPA?
JPA is the standard specification (jakarta.persistence annotations and interfaces), Hibernate is its most widely used implementation, and Spring Data JPA generates repositories and queries on top of JPA.
Do I need to know SQL to follow this course?
Yes, a grounding in relational modeling and SQL (tables, foreign keys, joins) is required: it helps you understand what Hibernate generates and avoid inefficient queries.
Which database is used?
The examples target PostgreSQL in production and in-memory H2 for experimenting. The JPA code is the same: only the data source configuration changes.
What is the N+1 problem?
It is when you run one query to load a list, then one additional query for each element to load a relationship. The course shows how to detect and fix it with join fetch, @EntityGraph, or projections.
This course, told for your job
Say what you want to be able to do: “Persistence with JPA and Hibernate” takes its place in your path, with examples from your field. The concepts stay those of the course.