hibernate/hibernate-orm · error · NullPointerException
null object passed to getCurrentLockMode()
Error message
null object passed to getCurrentLockMode()
What it means
Session.getCurrentLockMode(object) reports the lock mode of a managed entity, but it has no meaningful answer for null — so Hibernate throws NullPointerException explicitly ('null object passed to getCurrentLockMode()') rather than letting a generic NPE surface later. After the null check the code unwraps proxies and looks up the EntityEntry, so a non-null managed object is a hard precondition.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java:546
}
/**
* clear all the internal collections, just
* to help the garbage collector, does not
* clear anything that is needed during the
* afterTransactionCompletion() phase
*/
@Override
protected void cleanupOnClose() {
persistenceContext.clear();
}
@Override
public LockMode getCurrentLockMode(Object object) {
checkOpen();
checkTransactionSyncStatus();
if ( object == null ) {
throw new NullPointerException( "null object passed to getCurrentLockMode()" );
}
final var lazyInitializer = extractLazyInitializer( object );
if ( lazyInitializer != null ) {
object = lazyInitializer.getImplementation( this );
if ( object == null ) {
return LockMode.NONE;
}
}
final var entry = getEntityEntry( object );
if ( entry.getStatus().isDeletedOrGone() ) {
throw new ObjectDeletedException( "Given entity was removed", entry.getId(),
entry.getPersister().getEntityName() );
}
else {
return entry.getLockMode();
}View on GitHub (pinned to fad1729dce)
Solutions
- Null-check the entity before asking for its lock mode: if (obj == null) return LockMode.NONE / handle
- Verify the entity actually loaded: use session.find() and check the result, or session.contains(obj)
- Use Optional chaining: session.find(...).map(o -> session.getCurrentLockMode(o)).orElse(LockMode.NONE)
- Enable -XX:+ShowHiddenFrames/parameter logging or assert inputs in debug builds to catch where null originates
Example fix
// before
Object order = session.get(Order.class, orderId);
LockMode mode = session.getCurrentLockMode(order); // NPE when orderId not found
// after
Order order = session.get(Order.class, orderId);
if (order == null) {
throw new EntityNotFoundException("Order " + orderId + " not found");
}
LockMode mode = session.getCurrentLockMode(order); Defensive patterns
Strategy: validation
Validate before calling
Objects.requireNonNull(object, "entity passed to getCurrentLockMode() must not be null"); LockMode mode = session.getCurrentLockMode(object);
Prevention
- Check load results before use: session.find() returns null for missing ids
- Avoid Optional.orElse(null) for entities flowing into session APIs
- Add @NonNull/Nullness annotations (JPA/Hibernate use them) so IDEs flag nullable args at compile time
When it happens
Trigger: Calling session.getCurrentLockMode(null) — usually because a variable holding the entity is null after a failed lookup (getReference with wrong id, a get() that returned null and was not checked).
Common situations: Chaining operations after session.find() without a null check; passing an Optional.orElse(null) result; refactoring that renames fields and leaves the parameter unset; test code exercising lock modes with placeholder nulls.
Related errors
- CacheMode cannot be null
- Given entity was removed
- WRITE is not a valid LockMode as an argument
- Cannot lazily initialize collection
- Illegal attempt to associate a collection with two open sess
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/91385268d4bb5ad7.
Report an issue: GitHub.