diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java index 06b4f1d33be9..593c744522fb 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java @@ -1247,31 +1247,23 @@ public NaturalIdMultiLoadAccess byMultipleNaturalId(String entityName) { @Override public Object load(LoadType loadType, Object id, String entityName, LockOptions lockOptions, Boolean readOnly) { - if ( lockOptions != null ) { - // TODO: I doubt that this branch is necessary, and it's probably even wrong - final var event = makeLoadEvent( entityName, id, readOnly, lockOptions ); + boolean success = false; + try { + final var event = + makeLoadEvent( entityName, id, readOnly, + lockOptions == null ? LockOptions.NONE : lockOptions ); fireLoad( event, loadType ); final Object result = event.getResult(); releaseLoadEvent( event ); + if ( !loadType.isAllowNulls() && result == null ) { + getSession().getFactory().getEntityNotFoundDelegate().handleEntityNotFound( entityName, id ); + } + success = true; return result; } - else { - boolean success = false; - try { - final var event = makeLoadEvent( entityName, id, readOnly, false ); - fireLoad( event, loadType ); - final Object result = event.getResult(); - releaseLoadEvent( event ); - if ( !loadType.isAllowNulls() && result == null ) { - getSession().getFactory().getEntityNotFoundDelegate().handleEntityNotFound( entityName, id ); - } - success = true; - return result; - } - finally { - // we might be called from outside transaction - afterOperation( success ); - } + finally { + // we might be called from outside transaction + afterOperation( success ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/loader/internal/IdentifierLoadAccessImpl.java b/hibernate-core/src/main/java/org/hibernate/loader/internal/IdentifierLoadAccessImpl.java index ac5d475e1f6f..5ab14cfcebab 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/internal/IdentifierLoadAccessImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/internal/IdentifierLoadAccessImpl.java @@ -138,13 +138,15 @@ protected T perform(Supplier executor) { } } - @SuppressWarnings( "unchecked" ) // Hibernate Reactive overrides this protected T doGetReference(Object id) { final var session = context.getSession(); final var concreteType = entityPersister.resolveConcreteProxyTypeForId( id, session ); - return (T) context.load( LoadEventListener.LOAD, coerceId( id, session.getFactory() ), - concreteType.getEntityName(), lockOptions, isReadOnly( session ) ); + final Object result = + context.load( LoadEventListener.LOAD, coerceId( id, session.getFactory() ), + concreteType.getEntityName(), lockOptions, isReadOnly( session ) ); + //noinspection unchecked + return (T) result; } // Hibernate Reactive might need to call this @@ -164,20 +166,21 @@ public Optional loadOptional(Object id) { return Optional.ofNullable( perform( () -> doLoad( id ) ) ); } - @SuppressWarnings( "unchecked" ) // Hibernate Reactive overrides this protected T doLoad(Object id) { final var session = context.getSession(); Object result; try { - result = context.load( LoadEventListener.GET, coerceId( id, session.getFactory() ), - entityPersister.getEntityName(), lockOptions, isReadOnly( session ) ); + result = + context.load( LoadEventListener.GET, coerceId( id, session.getFactory() ), + entityPersister.getEntityName(), lockOptions, isReadOnly( session ) ); } catch (ObjectNotFoundException notFoundException) { - // if session cache contains proxy for non-existing object + // if session cache contains proxy for nonexisting object result = null; } initializeIfNecessary( result ); + //noinspection unchecked return (T) result; }