What this animation explains
The Java API remains stable while URL, driver and dialect change.

MODULE 3 · TOPIC 1 · VISUAL JPA & HIBERNATE LAB
Jakarta Persistence (JPA) defines the contract for mapping Java objects to relational data. Hibernate ORM implements that contract and uses JDBC drivers to talk to the database. A persistence unit supplies mapping and connection settings; an EntityManager owns a persistence context that tracks entity identity and changes. Learn how metadata, SQL, transactions and object state cooperate from startup to commit. This course uses Java 21, Jakarta Persistence 3.1 and Hibernate ORM 6.6 as a fixed teaching baseline.
Browser-based JPA and Hibernate simulations. Change inputs and trace entity state, SQL, database rows and transactions. No real Java or database operations are executed.
Object-relational mapping translates fields and object identity into columns and keys. JPA supplies the API; Hibernate interprets the mapping, chooses SQL and delegates to a JDBC driver. The database enforces constraints and transaction semantics. A read can also be served from a persistence context instead of SQL; this lab assumes an initially empty context.
Why this example? Select a database and a read or write operation to trace each layer and inspect the corresponding database rows.
Arrows show the actual route; blue identifies the current node. Green indicates visited operations; red nodes show errors or rejected paths.
Change inputs and step through the example. SQL is illustrative; actual Hibernate SQL, timing and dialect vary. Reproduce using the real setup described in the worked examples.
The Java API remains stable while URL, driver and dialect change.
Switch H2 to MySQL, then change find to persist. Follow the data through the driver and database cylinder.
An entity is a persistent Java class with an identity. Field access is determined by where the identifier annotation is placed unless explicitly overridden. Column names, constraints and ID generation define the mapping, while the relational database stores actual values.
Why this example? Change the ID strategy and the nullable attribute to see how the example schema changes.
Arrows show the actual route; blue identifies the current node. Green indicates visited operations; red nodes show errors or rejected paths.
Change inputs and step through the example. SQL is illustrative; actual Hibernate SQL, timing and dialect vary. Reproduce using the real setup described in the worked examples.
Metadata bridges objects and rows; schema generation is not a migration tool.
Compare assigned IDs and identity generation, then inspect the name constraint.
A resource-local persistence unit configures provider, entity metadata, JDBC URL, credentials and schema policy. The factory is relatively expensive and commonly reused for the application lifetime. An EntityManager is shorter-lived. Connection or dependency problems must be resolved before running persistence operations.
Why this example? Toggle driver availability and database reachability; inspect the successful or failed startup path.
Arrows show the actual route; blue identifies the current node. Green indicates visited operations; red nodes show errors or rejected paths.
Change inputs and step through the example. SQL is illustrative; actual Hibernate SQL, timing and dialect vary. Reproduce using the real setup described in the worked examples.
Bootstrap connects configuration to runtime objects, not to a generic Java compiler.
Try missing driver first, then reachable server with driver present.
A newly constructed entity is transient; persist makes a new entity managed, find loads a managed entity, detach or context close ends tracking, and remove marks a managed entity for deletion. Managed modifications can be synchronized automatically. Detached changes require a deliberate merge or another managed update.
Why this example? Modify the same student with and without detachment, then commit or roll back.
Arrows show the actual route; blue identifies the current node. Green indicates visited operations; red nodes show errors or rejected paths.
Change inputs and step through the example. SQL is illustrative; actual Hibernate SQL, timing and dialect vary. Reproduce using the real setup described in the worked examples.
The entity lifecycle decides whether Java changes become persistent changes.
Compare commit while managed with commit after detach; both Java objects have marks 90 but database results differ.
Within one persistence context, there is at most one managed entity instance for a given persistent identity. This property enables coherent updates and avoids repeated identifier loads. A different EntityManager has a separate identity map; this lab disables L2 cache and assumes a stable database.
Why this example? Change context and identifier to compare object reference equality and SQL counts.
Arrows show the actual route; blue identifies the current node. Green indicates visited operations; red nodes show errors or rejected paths.
Change inputs and step through the example. SQL is illustrative; actual Hibernate SQL, timing and dialect vary. Reproduce using the real setup described in the worked examples.
Identity reuse is different from database value equality or caching a missing record.
Use an existing ID twice in one context, then separate contexts. Try a missing ID to see null.
Hibernate can validate a schema or generate it for development. Validation detects mapped schema mismatches without repairing them. A destructive schema action such as create-drop is appropriate only for disposable test databases. Real application schemas should evolve with controlled migrations.
Why this example? Choose schema policy and existing schema condition; observe startup result and schema action.
Arrows show the actual route; blue identifies the current node. Green indicates visited operations; red nodes show errors or rejected paths.
Change inputs and step through the example. SQL is illustrative; actual Hibernate SQL, timing and dialect vary. Reproduce using the real setup described in the worked examples.
Schema configuration controls database lifecycle and can destroy data if misused.
Try validate on a missing schema, then select create-drop only in the virtual test lab.
Refer to these APIs and settings while building the real JPA project. Use a transaction for write operations.
| API / setting | Why and when to use it |
|---|---|
| @Entity / @Id | Declare a persistent type and its identifier |
| Persistence.createEntityManagerFactory | Bootstrap a persistence unit |
| emf.createEntityManager() | Open one persistence context |
| em.find(Student.class, id) | Lookup by identifier |
| em.flush() | Synchronize managed changes to SQL within the transaction |
| em.close() | Close the context; managed entities become detached |
Follow these procedures with the Jakarta Persistence 3.1 / Hibernate ORM 6.6 teaching project. Snippets require the indicated entities and an open EntityManager; SQL varies by dialect.
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>in.makeacademy</groupId><artifactId>jpa-lab</artifactId><version>1.0</version>
<properties><maven.compiler.release>21</maven.compiler.release></properties>
<dependencies>
<dependency><groupId>org.hibernate.orm</groupId><artifactId>hibernate-core</artifactId><version>6.6.18.Final</version></dependency>
<dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><version>2.3.232</version><scope>runtime</scope></dependency>
</dependencies>
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.13.0</version></plugin></plugins></build>
</project>
<!-- Separate file: src/main/resources/META-INF/persistence.xml -->
<persistence xmlns="https://jakarta.ee/xml/ns/persistence" version="3.1">
<persistence-unit name="school" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>demo.Student</class>
<properties>
<property name="jakarta.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="jakarta.persistence.jdbc.url" value="jdbc:h2:mem:school;DB_CLOSE_DELAY=-1"/>
<property name="jakarta.persistence.jdbc.user" value="sa"/>
<property name="jakarta.persistence.jdbc.password" value=""/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>package demo;
import jakarta.persistence.*;
@Entity @Table(name="student")
public class Student {
@Id public Long id;
@Column(nullable=false) public String name;
public int marks;
public Student() {}
public Student(Long id, String name, int marks) {
this.id=id; this.name=name; this.marks=marks;
}
}
// Separate file: App.java
package demo;
import jakarta.persistence.*;
public class App {
public static void main(String[] args) {
try (var emf=Persistence.createEntityManagerFactory("school");
var em=emf.createEntityManager()) {
var tx=em.getTransaction();
try {
tx.begin();
em.persist(new Student(101L,"Anu",80));
tx.commit();
System.out.println(em.find(Student.class,101L).name);
} catch (RuntimeException ex) {
if(tx.isActive()) tx.rollback();
throw ex;
}
}
}
}Try answering these questions, then expand each answer and repeat the appropriate simulation.
No. Hibernate typically uses JDBC underneath to execute SQL.
Its persistence context is mutable and not thread-safe.
No. Commit finalizes a successful database transaction.
Build the H2 project in the worked example. Change the entity field and inspect the SQL produced by Hibernate; compare a managed object with a detached object.
Edit this practice scratchpad and copy it into your real Java project. The browser does not compile arbitrary Java.
JPA is a specification, not a database or driver. Hibernate is an implementation; use jakarta.persistence imports for this baseline. SQL and flush timing depend on the provider, identifier strategy, dialect and configuration. Use create-drop only in disposable lab databases.