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

  1. Null-check the entity before asking for its lock mode: if (obj == null) return LockMode.NONE / handle
  2. Verify the entity actually loaded: use session.find() and check the result, or session.contains(obj)
  3. Use Optional chaining: session.find(...).map(o -> session.getCurrentLockMode(o)).orElse(LockMode.NONE)
  4. 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

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


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/91385268d4bb5ad7. Report an issue: GitHub.