From cf386a1b4789dc4ca16452029fbb68b5f24d8473 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 5 Mar 2024 12:48:45 +0100 Subject: [PATCH] HHH-16672 Locking entities created in the same transaction fails --- .../org/hibernate/loader/ast/internal/LoaderHelper.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/LoaderHelper.java b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/LoaderHelper.java index f1bd96a3c7..f2c0763412 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/LoaderHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/LoaderHelper.java @@ -107,7 +107,12 @@ public class LoaderHelper { entry.forceLocked( object, nextVersion ); } else { - persister.lock( entry.getId(), entry.getVersion(), object, lockOptions, session ); + if ( entry.isExistsInDatabase() ) { + persister.lock( entry.getId(), entry.getVersion(), object, lockOptions, session ); + } + else { + session.forceFlush( entry ); + } } entry.setLockMode(requestedLockMode); }