Entities, EntityManager & the Persistence Context
A Specification, Not an Implementation
JPA defines a standard set of interfaces/annotations for mapping Java objects to a relational database, without itself being a concrete implementation. Hibernate is the most popular implementation of that spec — code written against JPA's standard API is, in principle, portable across different providers (Hibernate, EclipseLink), reducing vendor lock-in.
Entities & the Primary Key
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String email;
}@Entity marks a class as mapped to a table (instance = row, field = column). @Id designates the primary key field, required on every entity for JPA to identify and manage individual instances.
EntityManager, Persistence Context & Dirty Checking
User user = entityManager.find(User.class, 1L);
user.setEmail("new@example.com"); // no explicit save call needed
// on transaction commit, JPA detects the change and issues UPDATEEntityManager is the primary interface for CRUD operations and queries. The persistence context is the set of entities it's currently tracking — changes to a tracked ("managed") entity are automatically detected via dirty checking and synchronized to the database on commit, no explicit update call needed.
Keep your own version of these notes — editable, searchable, and organised by your stack.
Start free