Skip to content
Ameen Abozaid
JNR-028

JPA and Hibernate

  • 8 Lessons
  • 4 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Entities
  • identifiers
  • relationships
  • entity lifecycle
  • dirty checking

Learn first

4 h of practice (estimate)

Learning outcomes

  1. Map entities with @Entity, @Id, generated identifiers, columns, enums, and embedded value objects.
  2. Describe entity states (transient, managed, detached, removed) and transitions.
  3. Explain dirty checking and flush timing, and predict when SQL is issued.
  4. Implement equals/hashCode safely for entities.
  5. Enable SQL logging and read the generated statements.

Lessons

  1. 01Mapping an entity
  2. 02Identifiers and generation strategies
  3. 03Embeddables and value objects
  4. 04Entity lifecycle
  5. 05Dirty checking and flushing
  6. 06Entity equality
  7. 07Demo: mapping Shop entities and predicting the SQL
  8. 08Demo: entity states in the debugger

Lab and exercises

Lab

JNR-028-LAB1 · Entities for Shop

Outcomes: 1–5 · Time: ~4 h

  1. Map Category and Product (with Money as an embeddable and a status enum) onto the existing schema.sql; keep ddl-auto=none.
  2. Enable SQL logging.
  3. In a transactional test run: create a product, update its price, read it again in a new transaction.
  4. For each statement in the log, write one sentence on why it ran at that moment.
  5. Write a safe equals/hashCode for Product and a test that adds it to a HashSet before and after saving.

Acceptance: the written explanation matches the SQL log statement by statement.

Exercises (assessment: "predict the SQL")

IDOutcomeTask
JNR-028-EX011Add @Entity, @Id, and column mappings to a Customer class with a status enum.
JNR-028-EX021Turn Address(street, city, zip) into an embeddable used by Customer.
JNR-028-EX032Label each object as transient, managed, detached, or removed in a given 10-line code listing.
JNR-028-EX043Predict the SQL (statements and order) for: load a product, change the price, call a JPQL query, commit.
JNR-028-EX054Fix an entity with Lombok @Data by writing correct equals/hashCode.
JNR-028-EX065Turn on SQL logging with parameter values and explain one logged insert.
JNR-028-EX071, 3Explain 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; in Customer.
  • EX03 new objects are transient; objects loaded or persisted inside the open transaction are managed; those from a closed context are detached; those after remove are removed.
  • EX04 select ... from product where id=?; then (auto flush before the query) update product set price=? where id=?; then the query select; commit has nothing left to flush.
  • EX05 Remove @Data; use equals on non-null id with getClass().hashCode(), or a stable business key such as sku.
  • EX06 logging.level.org.hibernate.SQL=DEBUG and logging.level.org.hibernate.orm.jdbc.bind=TRACE (verify property names for your Hibernate version).
  • EX07 With IDENTITY the id exists only after the insert, so Hibernate must send each insert immediately; SEQUENCE lets 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.

  1. 1The safe way to store an enum is:O1
  2. 2An @Embeddable is stored:O1
  3. 3GenerationType.IDENTITY has the drawback of:O1
  4. 4Entities should be returned directly from controllers:O1
  5. 5An object created with new Product() is:O2
  6. 6After the transaction ends, a loaded entity is:O2
  7. 7Dirty checking means Hibernate:O3
  8. 8p.setName("X") on a managed entity inside a transaction:O3
  9. 9Why is Lombok @Data risky on entities?O4
  10. 10To see generated SQL you:O5

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.

Type to search levels, phases, modules and lessons.