From ed107f843676bf1978b86d9dc413f3ea7e9e1410 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Mon, 6 Nov 2023 09:21:09 +0100 Subject: [PATCH] HHH-17380 Persisting an entity with a non generated id and @MapsId throws PropertyValueException --- .../entity/AbstractEntityPersister.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java index b26a71c195..9d9852ea10 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java @@ -105,6 +105,7 @@ import org.hibernate.generator.internal.VersionGeneration; import org.hibernate.graph.spi.RootGraphImplementor; import org.hibernate.id.Assigned; import org.hibernate.id.BulkInsertionCapableIdentifierGenerator; +import org.hibernate.id.ForeignGenerator; import org.hibernate.id.IdentifierGenerator; import org.hibernate.id.OptimizableGenerator; import org.hibernate.id.PostInsertIdentityPersister; @@ -3924,13 +3925,16 @@ public abstract class AbstractEntityPersister return false; } } - final Boolean unsaved = identifierMapping.getUnsavedStrategy().isUnsaved( id ); - if ( unsaved != null && !unsaved ) { - throw new PropertyValueException( - "Detached entity with generated id '" + id + "' has an uninitialized version value '" + version + "'", - getEntityName(), - getVersionColumnName() - ); + final Generator identifierGenerator = getGenerator(); + if ( identifierGenerator != null && !( identifierGenerator instanceof ForeignGenerator ) ) { + final Boolean unsaved = identifierMapping.getUnsavedStrategy().isUnsaved( id ); + if ( unsaved != null && !unsaved ) { + throw new PropertyValueException( + "Detached entity with generated id '" + id + "' has an uninitialized version value '" + version + "'", + getEntityName(), + getVersionColumnName() + ); + } } } return isUnsaved;