Week 3 — Hibernate / JPA: Data Access Layer & CRUD¶
Goal¶
Understand how Spring Boot integrates the JPA specification and Hibernate ORM to persist Java objects to relational databases. By the end of this week you should be able to wire a complete DAO layer using EntityManager, write JPQL queries for read operations, and understand when to reach for Spring Data JPA's JpaRepository as a higher-level abstraction.
Week 3 Schedule¶
| Topic | Lectures | Key Annotations / Concepts |
|---|---|---|
| Hibernate / JPA overview | Slides 1–10 | ORM, JPA spec, Hibernate as vendor, JDBC beneath |
| Entity mapping & identity | Slides 11–22 | @Entity, @Table, @Id, @GeneratedValue, @Column |
| DAO pattern with EntityManager | Slides 23–40 | @Repository, @Transactional, EntityManager.persist() |
| Reading: findById & findAll | Slides 41–55 | EntityManager.find(), TypedQuery, JPQL FROM |
| JPQL queries & named params | Slides 56–68 | :named, setParameter(), LIKE, ORDER BY |
| Update & delete operations | Slides 69–85 | EntityManager.merge(), EntityManager.remove() |
| Schema generation options | Slides 86–95 | spring.jpa.hibernate.ddl-auto |
| EntityManager vs JpaRepository | Slides 96–100 | When to use each; boilerplate elimination |
Week 3 Content¶
-
Topic Notes — Part 1
JPA / Hibernate overview, ORM concepts, the EntityManager, entity class mapping (
@Entity,@Table,@Id,@GeneratedValue), DAO pattern,@Repository,@Transactional, and the full CRUD save/read cycle. -
Topic Notes — Part 2
JPQL — query language syntax,
TypedQuery, named parameters, ordering; update and delete viaEntityManager.merge()/EntityManager.remove(); bulk JPQL DML; DDL auto-generation configuration; EntityManager vs JpaRepository comparison. -
Book Reading — Spring in Action Ch 3
Craig Walls on working with data: JdbcTemplate without Hibernate, JDBC repositories, schema / data SQL bootstrapping, Spring Data JDBC, transitioning to Spring Data JPA,
CrudRepository, andJpaRepository. -
Lab Guide — Student Information System DAO Tier
Full CRUD against a MySQL
studenttable usingEntityManager,@Repository,@Transactional, JPQLfindAll/findByLastName, update withmerge(), and delete. -
Week 3 Summary
Annotation quick-reference, ORM concept map, EntityManager method table, JPQL cheat sheet,
ddl-autooptions table, EntityManager vs JpaRepository comparison, and prerequisite checklist for Week 4.
Week 3 Progress¶
- Explain the difference between JPA (spec) and Hibernate (implementation)
- Annotate a class with
@Entity,@Table,@Id,@GeneratedValue,@Column - Explain what Spring Boot auto-configures when
spring-boot-starter-data-jpais on the classpath - Inject
EntityManagervia constructor injection into a@Repositoryclass - Implement
save()withentityManager.persist()and@Transactional - Implement
findById()withentityManager.find()— no@Transactionalneeded - Write a JPQL
FROM Studentquery usingTypedQueryto retrieve a list - Write a parameterized JPQL query using
:theDatanamed parameter andsetParameter() - Implement
update()withentityManager.merge()and@Transactional - Implement
delete()withentityManager.find()+entityManager.remove()and@Transactional - Explain the difference between
ddl-auto=create,update,validate, andnone - Articulate when to use
EntityManagerdirectly vsJpaRepository - Complete Week 3 Lab: Student Information System DAO Tier