Module 16: Database Integration and Persistence
JPA and Entity Mapping Basics
Use JPA as an object-relational mapping tool without forgetting what the database is doing underneath. This lesson introduces entities, identifiers, the persistence context, and the entity lifecycle so later Spring Data or Hibernate behavior feels predictable instead of magical.
Author
Java Learner Editorial Team
Reviewer
Technical review by Java Learner
Last reviewed
2026-04-17
Java version
Java 25 LTS
Learning goals
- Understand what makes a class an entity and how identifiers fit in
- Describe the basic entity lifecycle from transient to managed and detached
- See how the persistence context changes the way updates are tracked and flushed
Before you start
- You are comfortable with classes, exceptions, and writing small multi-class Java programs
Lesson roadmap: Start with the mental model, then follow the design choices, common pitfalls, and the practical workflow you should apply in a real project.
JPA is an object-relational mapping layer: Instead of writing all persistence logic by hand, you map classes to tables and let the framework manage much of the translation.
An entity represents a persistable domain object: It is a class whose instances correspond to rows in a table.
@Entity and @Id are foundational: They identify the class as managed persistent data and define its primary key.
Important mindset: JPA reduces boilerplate, but it does not remove the need to understand SQL, transactions, and database behavior.
Entity mindset: An entity is more than a plain data class; it represents a domain object whose identity matters over time. Two entities can have the same field values but still represent different rows because identity is part of the model.
Persistence context: JPA keeps track of managed entities inside a unit of work. That tracking is why a changed field can later be written back to the database even when you never call an explicit update statement.
Lifecycle states: New objects begin transient, become managed when attached to the persistence context, may become detached when the context ends, and are removed when scheduled for deletion. Knowing those states explains a lot of surprising ORM behavior.
Good boundary rule: Do not let every part of the application manipulate entities casually. Decide where entities live and where DTOs or service models should take over.
How to study this module: Keep a simple domain in mind, such as users, orders, or tasks. It is easier to understand JDBC, transactions, and ORM mapping when every SQL statement serves a clear business action.
Code review mindset: Database code is not only about “does it work.” Reviewers also look for resource safety, transaction boundaries, predictable query counts, and whether the persistence layer leaks too much SQL detail into higher layers.
Production habit: Measure query behavior early. Slow queries, long transactions, and leaked connections usually hurt real systems more than syntax mistakes do.
Runnable examples
A minimal entity class
@Entity
class User {
@Id
private Long id;
private String email;
}Expected output
The class is mapped as a persistable entity with a primary key.
A generated identifier is common for application-owned records
@Entity
class Task {
@Id
@GeneratedValue
private Long id;
private String title;
}Expected output
The ORM can assign a primary key when the entity is persisted.
Common mistakes
Thinking JPA removes the need to understand SQL or transactions
ORM reduces boilerplate, but query behavior, transaction boundaries, and database constraints still matter.
Treating detached entities as if they are still automatically tracked
Know whether the object is inside an active persistence context before assuming changes will be saved.
Mini exercise
Create an entity sketch for `Invoice` with an ID, a customer name, and a total amount, then explain in one paragraph what it means for that entity to be “managed” by the persistence context.
Summary
- JPA maps classes and objects to relational tables and rows.
- Entities are the core persistent domain types.
- ORM helps, but database understanding still matters.
- JPA becomes easier once you understand identity and the persistence context.
- Entity lifecycle state explains much of the framework behavior that surprises beginners.
Next step
Next, model relationships carefully so the object graph matches the data model without surprise queries.
Sources used