How do you implement pessimistic locking in JPA?
Use LockModeType.PESSIMISTIC_WRITE with EntityManager or Spring Data @Lock, set timeouts, avoid deadlocks and handle lock failures.
2,886 posts about Java: questions with answers, tutorials and articles, newest first.
Use LockModeType.PESSIMISTIC_WRITE with EntityManager or Spring Data @Lock, set timeouts, avoid deadlocks and handle lock failures.
@OptimisticLocking is a Hibernate annotation that sets how concurrent updates are detected. Learn its types, how it differs from JPA @Version and how to handle conflicts.
Add a @Version field, send it with each update and handle OptimisticLockingFailureException to prevent lost updates in JPA.
The JPA orphanRemoval attribute deletes a child when it is removed from its parent. Compare it with cascade REMOVE and avoid common Hibernate errors.
JPA has no update cascade type. Use CascadeType.MERGE for detached graphs and dirty checking for managed entities, as shown in this practical guide.
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.