Skip to content

harman-04/jpa-entity-manager-basics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JPA Core: Manual Entity Management

Project Overview

This project steps back from "Spring Magic" to show how the Jakarta Persistence API (JPA) actually works under the hood. It demonstrates the explicit lifecycle management of Java entities—from creation and persistence to querying—using the EntityManager.


Technical Concepts

1. The Persistence Unit

Configured in persistence.xml, the Persistence Unit is the configuration set that defines how the application connects to the database.

  • RESOURCE_LOCAL: Indicates that we are managing transactions manually in our code rather than relying on a Java EE Application Server.
  • JAXB Runtime: Included in the pom.xml because modern Java versions (like your Java 25) have removed the built-in XML binding libraries needed to parse the persistence config.

2. EntityManager vs. EntityManagerFactory

  • EntityManagerFactory: An expensive, thread-safe object created once per application lifetime. It represents the database structure.
  • EntityManager: A lightweight object used for a single "unit of work." It is the primary interface used to interact with the persistence context.

3. Explicit Transaction Management

In this project, you see the begin() and commit() calls.

  • Manual Control: Unlike @Transactional in Spring, here we manually wrap our work in transactions to ensure Atomicity (either everything saves, or nothing does).

Component Reference

Product.java (The Entity)

  • @Entity: Marks the class for optimization by the persistence provider (Hibernate).
  • GenerationType.IDENTITY: Tells MySQL to use its AUTO_INCREMENT feature to handle primary keys.

MainApp.java (The Logic)

This class contains the lifecycle logic:

  • persist(): Moves an object from the "Transient" state to the "Managed" state.
  • JPQL Query: Uses SELECT p FROM Product p. Notice we query the Class Name (Product), not the table name (products). JPA translates this to SQL automatically.

persistence.xml

The heart of the connection.

  • hibernate.hbm2ddl.auto = update: Automatically creates or modifies the MySQL table structure to match your Java class.
  • SSL REQUIRED: Configured for secure connection to cloud-hosted MySQL (Aiven).

️ 2026 Tech Stack

  • Java 25: Utilizing the latest JVM features.
  • Spring Boot 4.0.1 (BOM): Used here primarily for dependency version management.
  • Hibernate 6.x: The most popular JPA implementation.
  • MySQL 9.3: The latest relational database driver.

Execution Flow

  1. Bootstrap: Persistence.createEntityManagerFactory("example-unit") reads your XML config.
  2. Session Start: An EntityManager is opened.
  3. Write: Two Product objects are created, "persisted" into the context, and "committed" to the MySQL cloud database.
  4. Read: A JPQL query fetches all records and prints them to the console.
  5. Cleanup: Both the Manager and Factory are closed to prevent memory leaks.

How to Test

  1. MySQL Setup: Ensure your Aiven MySQL instance is running and accessible.
  2. Run: Execute the MainApp.
  3. Verify:
    • Watch the console. Because hibernate.show_sql is true, you will see the exact INSERT and SELECT statements Hibernate generates.
    • Example Output:
      Hibernate: insert into products (name, price) values (?, ?)
      Products
      ID: 1, Name: Product-1, Price: 10.99
      

About

A deep-dive into Jakarta Persistence (JPA) using Hibernate and MySQL. Demonstrates manual transaction management, EntityManager operations, and JAXB integration on Java 25.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages