From 29848043b65a7189b288740447bc6f75f25d960d Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 3 May 2022 17:39:51 +0200 Subject: [PATCH] HHH-15229 Query returns null for broken foreign key reference and NotFoundAction.EXCEPTION --- .../main/java/org/hibernate/internal/SessionImpl.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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 20bdec761f..20f6386187 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java @@ -25,6 +25,7 @@ import java.util.Set; import jakarta.persistence.metamodel.Metamodel; import org.hibernate.CacheMode; import org.hibernate.ConnectionAcquisitionMode; +import org.hibernate.FetchNotFoundException; import org.hibernate.Filter; import org.hibernate.FlushMode; import org.hibernate.HibernateException; @@ -2332,7 +2333,14 @@ public class SessionImpl return loadAccess.load( primaryKey ); } - catch ( EntityNotFoundException ignored ) { + catch (EntityNotFoundException entityNotFoundException) { + /* + This may happen if the entity has an associations mapped with @NotFound(action = NotFoundAction.EXCEPTION) + and this associated entity is not found. + */ + if ( entityNotFoundException instanceof FetchNotFoundException ) { + throw entityNotFoundException; + } // DefaultLoadEventListener#returnNarrowedProxy() may throw ENFE (see HHH-7861 for details), // which find() should not throw. Find() should return null if the entity was not found. if ( log.isDebugEnabled() ) {