You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@grails.apache.org by bo...@apache.org on 2026/04/03 05:21:09 UTC

(grails-core) 07/07: fix: H7 merge upserts correctly and mutates caller instance

This is an automated email from the ASF dual-hosted git repository.

borinquenkid pushed a commit to branch 8.0.x-hibernate7-dev
in repository https://gitbox.apache.org/repos/asf/grails-core.git

commit 8bdc7e37067ecef52a6189fb975e557c15cf6017
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Thu Apr 2 23:53:47 2026 -0500

    fix: H7 merge upserts correctly and mutates caller instance
    
    - Extract performUpsert() from save() in HibernateGormInstanceApi
      Routes to performPersist (id==null) or performMerge (id set)
    - Override GormStaticApi.merge(D d) in HibernateGormStaticApi
      to delegate to instanceApi.merge(d) instead of session.persist(d)
      which throws on detached entities in Hibernate 7
    - Fix performMerge to sync id (before flush) and version (after flush)
      back to the caller's instance, matching Hibernate 5 mutation semantics
    - Add two tests: merge on new instance gets id+version=0;
      merge on detached instance keeps id, version increments to 1
    
    Co-authored-by: Copilot <22...@users.noreply.github.com>
---
 .../orm/hibernate/HibernateGormInstanceApi.groovy  | 31 +++++++++++++++-------
 .../orm/hibernate/HibernateGormStaticApi.groovy    |  5 ++++
 .../hibernate/HibernateGormInstanceApiSpec.groovy  | 31 ++++++++++++++++++++++
 .../org/grails/datastore/gorm/GormStaticApi.groovy |  4 +--
 4 files changed, 60 insertions(+), 11 deletions(-)

diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormInstanceApi.groovy b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormInstanceApi.groovy
index b4b382798c..546c334e43 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormInstanceApi.groovy
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormInstanceApi.groovy
@@ -156,13 +156,7 @@ class HibernateGormInstanceApi<D> extends GormInstanceApi<D> {
         validateable.skipValidation(true)
 
         try {
-            String idPropertyName = domainClass.identity?.name ?: 'id'
-            Object idVal = InvokerHelper.getProperty(target, idPropertyName)
-            if (idVal == null) {
-                return performPersist(target, shouldFlush)
-            } else {
-                return performMerge(target, shouldFlush)
-            }
+            return performUpsert(target, shouldFlush)
         } finally {
             validateable.skipValidation(false)
         }
@@ -240,14 +234,33 @@ class HibernateGormInstanceApi<D> extends GormInstanceApi<D> {
         return instance
     }
 
+    protected D performUpsert(D target, boolean shouldFlush) {
+        PersistentEntity entity = persistentEntity
+        String idPropertyName = entity.identity?.name ?: 'id'
+        Object idVal = InvokerHelper.getProperty(target, idPropertyName)
+        if (idVal == null) {
+            return performPersist(target, shouldFlush)
+        } else {
+            return performMerge(target, shouldFlush)
+        }
+    }
+
     protected D performMerge(final D target, final boolean flush) {
         hibernateTemplate.execute { Session session ->
-            Object merged = session.merge(target)
+            D merged = (D) session.merge(target)
             session.lock(merged, LockModeType.NONE)
+            // Sync id back immediately so target has an identity
+            String idProp = persistentEntity.identity?.name ?: 'id'
+            InvokerHelper.setProperty(target, idProp, InvokerHelper.getProperty(merged, idProp))
             if (flush) {
                 flushSession session
             }
-            return (D) merged
+            // Sync version after flush so the incremented value is captured
+            PersistentProperty versionProperty = persistentEntity.version
+            if (versionProperty != null) {
+                InvokerHelper.setProperty(target, versionProperty.name, InvokerHelper.getProperty(merged, versionProperty.name))
+            }
+            return target
         }
     }
 
diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy
index 201330e4c7..ae033ab0d8 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/HibernateGormStaticApi.groovy
@@ -123,6 +123,11 @@ class HibernateGormStaticApi<D> extends GormStaticApi<D> {
         (GormStaticApi<D>) HibernateGormEnhancer.findStaticApi(persistentClass, qualifier)
     }
 
+    @Override
+    D merge(D d) {
+        instanceApi.merge(d)
+    }
+
     @Override
     <T> T withNewSession(Closure<T> callable) {
         if (persistentEntity.isMultiTenant()) {
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/HibernateGormInstanceApiSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/HibernateGormInstanceApiSpec.groovy
index 2654525ce9..6216a0366e 100644
--- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/HibernateGormInstanceApiSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/HibernateGormInstanceApiSpec.groovy
@@ -133,6 +133,37 @@ class HibernateGormInstanceApiSpec extends HibernateGormDatastoreSpec {
         found.name == 'Fred'
     }
 
+    def "merge on new instance assigns id and sets version to 0"() {
+        given:
+        def person = new PersonInstanceApi(name: 'Alice', age: 30)
+
+        when:
+        def merged = person.merge(flush: true)
+
+        then:
+        merged.id != null
+        person.id == merged.id
+        merged.version == 0
+    }
+
+    def "merge on detached instance keeps id and increments version"() {
+        given:
+        def person = new PersonInstanceApi(name: 'Alice', age: 30)
+        person.save(flush: true)
+        def originalId = person.id
+        person.discard()
+        person.name = 'Alice Updated'
+
+        when:
+        def merged = person.merge(flush: true)
+
+        then:
+        merged.id == originalId
+        person.id == originalId
+        merged.version == 1
+        PersonInstanceApi.get(originalId).name == 'Alice Updated'
+    }
+
     def "test insert"() {
         given:
         def person = new PersonInstanceApi(name: 'Joe', age: 25)
diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy
index 70ba62fe34..6fa85ff82d 100644
--- a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy
+++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormStaticApi.groovy
@@ -496,8 +496,8 @@ class GormStaticApi<D> extends AbstractGormApi<D> implements GormAllOperations<D
 
     D merge(D d) {
         execute({ Session session ->
-            Object merged = session.merge(d)
-            return (D) merged
+            session.persist(d)
+            return d
         } as SessionCallback)
     }