Skip to content

Commit 40de05e

Browse files
committed
HHH-15229 Query returns null for broken foreign key reference and NotFoundAction.EXCEPTION
1 parent 0d9a120 commit 40de05e

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import jakarta.persistence.metamodel.Metamodel;
2626
import org.hibernate.CacheMode;
2727
import org.hibernate.ConnectionAcquisitionMode;
28+
import org.hibernate.FetchNotFoundException;
2829
import org.hibernate.Filter;
2930
import org.hibernate.FlushMode;
3031
import org.hibernate.HibernateException;
@@ -2332,7 +2333,14 @@ public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode
23322333

23332334
return loadAccess.load( primaryKey );
23342335
}
2335-
catch ( EntityNotFoundException ignored ) {
2336+
catch (EntityNotFoundException entityNotFoundException) {
2337+
/*
2338+
This may happen if the entity has an associations mapped with @NotFound(action = NotFoundAction.EXCEPTION)
2339+
and this associated entity is not found.
2340+
*/
2341+
if ( entityNotFoundException instanceof FetchNotFoundException ) {
2342+
throw entityNotFoundException;
2343+
}
23362344
// DefaultLoadEventListener#returnNarrowedProxy() may throw ENFE (see HHH-7861 for details),
23372345
// which find() should not throw. Find() should return null if the entity was not found.
23382346
if ( log.isDebugEnabled() ) {

0 commit comments

Comments
 (0)