Day 08 — JPA 3.1 Entity Mapping & Relationship Boundaries¶
Daily Time Investment: 2.5 hours | Week: 2 | Phase: 3
Daily Schedule¶
| Segment | Duration | Activity |
|---|---|---|
| Core Theory | 45 min | @Entity, @Table, @Id, @GeneratedValue, all 4 cardinalities, @Embeddable, @ElementCollection, cascade lifecycle, orphanRemoval, Lazy vs Eager fetching |
| Book Reading | 30 min | Pro Persistence Ch4 (ORM) + Ch5 (Collection Mapping) + EE8 AppDev Ch3 |
| Hands-On Lab | 75 min | Multi-entity cluster schema (ServerRack, ClusterNode, TelemetryRecord, SecurityGroup); verify LazyInitializationException at context boundary |
Files in This Day¶
-
Pro Persistence Ch4 & Ch5 — Object-Relational Mapping & Collections
@Entity,@Table,@Id,@GeneratedValuestrategies,@Column,@Embeddable,@ElementCollection, all 4 relationship cardinalities,@JoinColumn,@JoinTable,CascadeType,FetchType,orphanRemoval,@OrderBy. -
Lab Guide — JPA Entity Mappings & Boundaries
Build the
ClusterNodedomain:ServerRack → ClusterNode → TelemetryRecord(1:N),ClusterNode ↔ SecurityGroup(N:M),ClusterNode ↔ NodeDiagnostics(1:1),HardwareSpec(@Embeddable),Set<String> tags(@ElementCollection). Verify cascade persist, orphan removal, andLazyInitializationException.
Prerequisites to Continue¶
New concepts not seen in Phase 1 or Phase 2
EntityManager&EntityManagerFactory— the central JPA API;EntityManagerFactoryis a thread-safe heavyweight factory created once per persistence unit;EntityManageris a lightweight, non-thread-safe unit-of-work instancepersistence.xml— the JPA configuration descriptor placed insrc/main/resources/META-INF/; declares the persistence unit name, JPA provider class (Hibernate), JDBC connection properties, and entity scanning- Hibernate ORM 6.4 — the JPA 3.1 reference implementation used in Jakarta EE 10; generates SQL DDL automatically when
hibernate.hbm2ddl.auto=create-drop - H2 In-Memory Database — a pure-Java embedded database; ideal for lab environments since zero installation is required; data vanishes when JVM exits
GenerationType.IDENTITY— relies onAUTO_INCREMENTin MySQL/H2; IDs are only available after the SQL INSERT fires (not beforepersist()returns)- Persistence Context — an in-memory cache of managed entity instances; not to be confused with Spring's
ApplicationContext— this is purely a JPA concept scoped to a transaction or stateful bean mappedBy— marks the inverse (non-owning) side of a bidirectional relationship; only the owning side has the physical@JoinColumnin the database; failing to setmappedBycreates duplicate join tables