hibernate/hibernate-orm · error · NullPointerException
Attempted to lock null
Error message
Attempted to lock null
What it means
Error "Attempted to lock null" thrown in hibernate/hibernate-orm.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLockEventListener.java:36
* Defines the default lock event listeners used by hibernate to lock entities
* in response to generated lock events.
*
* @author Steve Ebersole
*/
public class DefaultLockEventListener implements LockEventListener {
/**
* Handle the given lock event.
*
* @param event The lock event to be handled.
*/
@Override
public void onLock(@Nonnull LockEvent event) {
final Object instance = event.getObject();
//noinspection ConstantValue
if ( instance == null ) {
throw new NullPointerException( "Attempted to lock null" );
}
final var lockMode = event.getLockMode();
if ( lockMode == LockMode.WRITE || lockMode == LockMode.UPGRADE_SKIPLOCKED ) {
throw new IllegalArgumentException( "Invalid lock mode '" + lockMode + "' passed to 'lock()'" );
}
final var source = event.getSession();
final Object entity = forceInitialize( instance, source );
//TODO: if instance was an uninitialized proxy, this is inefficient,
// resulting in two SQL selects
final var entry = source.getPersistenceContextInternal().getEntry( entity );
if ( entry == null && instance == entity ) {
throw new DetachedObjectException( "Given entity is not associated with the persistence context" );
}
assert entry != null;
View on GitHub (pinned to fad1729dce)
Solutions
- Check the entity reference for null before calling lock().
- Guard with Optional or an explicit null check before invoking Session.lock().
When it happens
Trigger: Thrown when a caller passes a null Attempted to lock null to the API guarded in DefaultLockEventListener.java:36.
Common situations: Typical situations: calling the method with an uninitialized variable; a lookup that returned null being passed straight through; missing null-checks in application code before invoking Hibernate.
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/72cba85df1286e57.
Report an issue: GitHub.