JPA Basics

Basic Java
≈ 2 min read

JPA defines the rules, the provider does the work

JPA defines a common API for working with persistent objects: how to declare an Entity, how to find or save an object, how to describe relationships, and how to execute queries. A persistence provider such as Hibernate supplies the concrete implementation of those rules.

That is why application code mostly works with types from jakarta.persistence, while the provider translates object operations into SQL and communicates with the database.

The central object is EntityManager

User user = entityManager.find(User.class, 42L);

if (user != null) {
    System.out.println(user.getName());
}

The find() method looks up an Entity by its type and primary key. Instead of manually reading columns from a ResultSet, the application receives a User object. This does not eliminate SQL or the relational model — JPA simply takes over much of the conversion between them and objects.

JPA works around a persistence context

Loaded and saved Entities usually exist inside a special context that knows which objects it currently manages. This lets JPA track changes and synchronize object state with the database at the appropriate time. We will examine this mechanism in detail on a separate page.

JPA does not mean “SQL is no longer needed.” Good JPA code still requires understanding tables, relationships, queries, and the cost of database access.