JPA and Hibernate
On this page 8 topics
Overview
Topics
- Entities
- identifiers
- relationships
- entity lifecycle
- dirty checking
Learn first
4 h of practice (estimate)
Learning outcomes
- Map entities with
@Entity,@Id, generated identifiers, columns, enums, and embedded value objects. - Describe entity states (transient, managed, detached, removed) and transitions.
- Explain dirty checking and flush timing, and predict when SQL is issued.
- Implement
equals/hashCodesafely for entities. - Enable SQL logging and read the generated statements.
Lessons
Lab and exercises
Lab
JNR-028-LAB1 · Entities for Shop
Outcomes: 1–5 · Time: ~4 h
- Map
CategoryandProduct(withMoneyas an embeddable and astatusenum) onto the existingschema.sql; keepddl-auto=none. - Enable SQL logging.
- In a transactional test run: create a product, update its price, read it again in a new transaction.
- For each statement in the log, write one sentence on why it ran at that moment.
- Write a safe
equals/hashCodeforProductand a test that adds it to aHashSetbefore and after saving.
Acceptance: the written explanation matches the SQL log statement by statement.
Exercises (assessment: "predict the SQL")
| ID | Outcome | Task |
|---|---|---|
| JNR-028-EX01 | 1 | Add @Entity, @Id, and column mappings to a Customer class with a status enum. |
| JNR-028-EX02 | 1 | Turn Address(street, city, zip) into an embeddable used by Customer. |
| JNR-028-EX03 | 2 | Label each object as transient, managed, detached, or removed in a given 10-line code listing. |
| JNR-028-EX04 | 3 | Predict the SQL (statements and order) for: load a product, change the price, call a JPQL query, commit. |
| JNR-028-EX05 | 4 | Fix an entity with Lombok @Data by writing correct equals/hashCode. |
| JNR-028-EX06 | 5 | Turn on SQL logging with parameter values and explain one logged insert. |
| JNR-028-EX07 | 1, 3 | Explain why IDENTITY disables insert batching and what to use instead. |
Show solutions
- EX01
@Entity @Table(name="customer"),@Id @GeneratedValue private Long id;,@Enumerated(EnumType.STRING) private CustomerStatus status;, protected no-arg constructor. - EX02
@Embeddable record Address(...)and@Embedded private Address address;inCustomer. - EX03
newobjects are transient; objects loaded or persisted inside the open transaction are managed; those from a closed context are detached; those afterremoveare removed. - EX04
select ... from product where id=?; then (auto flush before the query)update product set price=? where id=?; then the queryselect; commit has nothing left to flush. - EX05 Remove
@Data; useequalson non-null id withgetClass().hashCode(), or a stable business key such assku. - EX06
logging.level.org.hibernate.SQL=DEBUGandlogging.level.org.hibernate.orm.jdbc.bind=TRACE(verify property names for your Hibernate version). - EX07 With
IDENTITYthe id exists only after the insert, so Hibernate must send each insert immediately;SEQUENCElets it fetch ids first and batch inserts.
Quiz
Outcomes: O1 entity mapping · O2 entity states · O3 dirty checking and flush · O4 equals/hashCode · O5 SQL logging.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Seven exercises JNR-028-EX01 to EX07 plus the lab; the assessment is the "predict the SQL" exercise (EX04). Pass: EX04 correct, at least five of seven exercises correct, and the lab explanation matches the log.
Common mistakes
Entities as API DTOs; GenerationType.IDENTITY without understanding batching impact; Lombok @Data on entities.
Assessment
"predict the SQL" exercise.
Career relevance
The default persistence stack for Spring services.