JPA Entity States: New, Managed, Detached and Removed
Understand the JPA entity lifecycle: what each state means, which EntityManager methods change it, and how it explains dirty checking and lazy loading errors.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
Understand the JPA entity lifecycle: what each state means, which EntityManager methods change it, and how it explains dirty checking and lazy loading errors.
JPA entities are transient, managed, detached or removed. Learn how persist, merge, remove and clear move them between states and when changes are saved.
@BatchSize makes Hibernate load lazy collections and entities in groups with IN queries. Learn how to use it, set a global default and avoid confusion with JDBC batching.
Speed up bulk inserts in JPA by enabling Hibernate JDBC batching, choosing a sequence generator, and flushing in chunks. Includes Spring Boot 3 examples.
Spring Data JPA repositories are already transactional by default. Learn when to add @Transactional yourself, for services and @Modifying queries.
Spring injects a shared, transaction-aware EntityManager. Learn how to customise the factory, add repository fragments and run a second persistence unit.
@PersistenceUnit injects an EntityManagerFactory by unit name. Learn when to use it, how it differs from @PersistenceContext and how to handle multiple units.
Create META-INF/persistence.xml with a named persistence-unit, provider, entity classes and JDBC properties, then open it with Persistence.createEntityManagerFactory.
Learn how to build an EntityManagerFactory once, create short-lived EntityManager instances, manage transactions, and let Spring Boot or Jakarta EE inject them.
EntityManagerFactory is the heavyweight, thread-safe factory of a JPA persistence unit that creates EntityManager instances. Create it once and close it at shutdown.
Define a JPA @NamedEntityGraph, apply it with fetchgraph or loadgraph or Spring Data @EntityGraph, and avoid N+1 queries without making associations eager.
@NamedEntityGraph defines a reusable fetch plan on a JPA entity. See how it prevents N+1 queries, how fetchgraph differs from loadgraph, and the pitfalls.
EntityGraph lets you choose which attributes JPA loads per query, avoiding N+1 selects without making associations eager.
Build dynamic JPA queries with CriteriaBuilder, optional predicates, allow-listed sorting, paging and joins, and make them compile-time safe with the static metamodel.
The JPA Predicate represents one condition in the Criteria API. Learn how to build dynamic, type-safe where clauses and combine them with and, or and not.
Use Root.join with JoinType.INNER or LEFT in the JPA Criteria API, add ON conditions, avoid N+1 queries with fetch joins, and use the metamodel.
CriteriaQuery is the JPA Criteria API interface for building type-safe, dynamic select queries in code. See a full example, joins, projections and the metamodel.
Build type-safe JPA criteria queries with CriteriaBuilder, Root and predicates. Examples cover dynamic filters, joins, aggregates and the static metamodel.
CriteriaBuilder is the JPA factory for type-safe, dynamic queries. See predicates, aggregates, joins, the static metamodel and when to prefer JPQL.
Build reusable, composable Specification methods in Spring Data JPA to run dynamic queries with optional filters, paging and joins.