This is an automated email from the ASF dual-hosted git repository. borinquenkid pushed a commit to branch fix/hibernate7-embedded-hasmany-collection-8.0.x in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 47b836c62860ed0a772d00acd6607b812540bf29 Author: Walter Duque de Estrada <[email protected]> AuthorDate: Sat Oct 10 21:54:11 2026 -0500 fix(hibernate7): bind each collection of an embedded entity with its own second pass An entity that is also embedded in another is bound twice, once as a component and once as an entity, and its hasMany property is bound with a different Hibernate collection each time. The property only remembers the last collection, and the second passes bound property.getCollection(), so the collection of the first binding never got its element and key and the session factory failed with 'Collection key not bound'. Each second pass now keeps the collection it was created for and restores it on the property before binding. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> (cherry picked from commit 7c65405620b4160641659093ebfa181cbe524312) --- .../cfg/domainbinding/binder/CollectionBinder.java | 6 +-- .../domainbinding/secondpass/ListSecondPass.java | 13 +++++ .../domainbinding/secondpass/MapSecondPass.java | 13 +++++ .../domainbinding/secondpass/SetSecondPass.java | 13 +++++ .../tests/EmbeddedEntityWithHasManySpec.groovy | 62 ++++++++++++++++++++++ 5 files changed, 104 insertions(+), 3 deletions(-) diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java index b0f1e07891..d36e871c26 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/CollectionBinder.java @@ -216,11 +216,11 @@ public class CollectionBinder { private void registerSecondPass(HibernateToManyProperty property, Collection collection) { if (collection instanceof org.hibernate.mapping.List) { - mappings.addSecondPass(new ListSecondPass(listSecondPassBinder, property)); + mappings.addSecondPass(new ListSecondPass(listSecondPassBinder, property, collection)); } else if (collection instanceof org.hibernate.mapping.Map) { - mappings.addSecondPass(new MapSecondPass(mapSecondPassBinder, property)); + mappings.addSecondPass(new MapSecondPass(mapSecondPassBinder, property, collection)); } else { - mappings.addSecondPass(new SetSecondPass(collectionSecondPassBinder, property)); + mappings.addSecondPass(new SetSecondPass(collectionSecondPassBinder, property, collection)); } } } diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPass.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPass.java index 3d5e6c832e..c9b2d5dbd5 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPass.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPass.java @@ -32,15 +32,28 @@ public class ListSecondPass implements org.hibernate.boot.spi.SecondPass, Grails private static final long serialVersionUID = -3024674993774205193L; protected final HibernateToManyProperty property; + private final org.hibernate.mapping.Collection collection; private final ListSecondPassBinder listSecondPassBinder; public ListSecondPass(ListSecondPassBinder listSecondPassBinder, HibernateToManyProperty property) { + this(listSecondPassBinder, property, null); + } + + /** + * @param collection the collection this pass binds. The same property can be bound more than once, for instance + * when its owner is an entity that is also embedded in another, and each binding has its own collection. + */ + public ListSecondPass(ListSecondPassBinder listSecondPassBinder, HibernateToManyProperty property, org.hibernate.mapping.Collection collection) { this.listSecondPassBinder = listSecondPassBinder; this.property = property; + this.collection = collection; } @Override public void doSecondPass(Map persistentClasses) throws MappingException { + if (collection != null) { + property.setHibernateCollection(collection); + } listSecondPassBinder.bindListSecondPass(property); createCollectionKeys(property.getCollection()); } diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/MapSecondPass.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/MapSecondPass.java index e21836c329..5acb7c5bf0 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/MapSecondPass.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/MapSecondPass.java @@ -32,15 +32,28 @@ public class MapSecondPass implements org.hibernate.boot.spi.SecondPass, GrailsS private static final long serialVersionUID = -3244991685626409031L; protected final HibernateToManyProperty property; + private final org.hibernate.mapping.Collection collection; private final MapSecondPassBinder mapSecondPassBinder; public MapSecondPass(MapSecondPassBinder mapSecondPassBinder, HibernateToManyProperty property) { + this(mapSecondPassBinder, property, null); + } + + /** + * @param collection the collection this pass binds. The same property can be bound more than once, for instance + * when its owner is an entity that is also embedded in another, and each binding has its own collection. + */ + public MapSecondPass(MapSecondPassBinder mapSecondPassBinder, HibernateToManyProperty property, org.hibernate.mapping.Collection collection) { this.mapSecondPassBinder = mapSecondPassBinder; this.property = property; + this.collection = collection; } @Override public void doSecondPass(Map persistentClasses) throws MappingException { + if (collection != null) { + property.setHibernateCollection(collection); + } mapSecondPassBinder.bindMapSecondPass(property); createCollectionKeys(property.getCollection()); } diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/SetSecondPass.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/SetSecondPass.java index 70755effe2..6bb05214b9 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/SetSecondPass.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/SetSecondPass.java @@ -38,15 +38,28 @@ public class SetSecondPass implements org.hibernate.boot.spi.SecondPass, GrailsS private static final long serialVersionUID = -5540526942092611348L; protected final HibernateToManyProperty property; + private final org.hibernate.mapping.Collection collection; private final CollectionSecondPassBinder collectionSecondPassBinder; public SetSecondPass(CollectionSecondPassBinder collectionSecondPassBinder, HibernateToManyProperty property) { + this(collectionSecondPassBinder, property, null); + } + + /** + * @param collection the collection this pass binds. The same property can be bound more than once, for instance + * when its owner is an entity that is also embedded in another, and each binding has its own collection. + */ + public SetSecondPass(CollectionSecondPassBinder collectionSecondPassBinder, HibernateToManyProperty property, org.hibernate.mapping.Collection collection) { this.collectionSecondPassBinder = collectionSecondPassBinder; this.property = property; + this.collection = collection; } @Override public void doSecondPass(Map persistentClasses) throws MappingException { + if (collection != null) { + property.setHibernateCollection(collection); + } collectionSecondPassBinder.bindCollectionSecondPass(property); createCollectionKeys(property.getCollection()); } diff --git a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/EmbeddedEntityWithHasManySpec.groovy b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/EmbeddedEntityWithHasManySpec.groovy new file mode 100644 index 0000000000..68da71ad57 --- /dev/null +++ b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/EmbeddedEntityWithHasManySpec.groovy @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package grails.gorm.tests + +import grails.gorm.annotation.Entity +import org.apache.grails.data.hibernate7.core.GrailsDataHibernate7TckManager +import org.apache.grails.data.testing.tck.base.GrailsDataTckSpec + +/** + * An entity that is also embedded in another can declare a hasMany. The collection is bound once for the + * embedded component and once for the entity itself, and each needs its own element and key. + */ +class EmbeddedEntityWithHasManySpec extends GrailsDataTckSpec<GrailsDataHibernate7TckManager> { + + void setupSpec() { + manager.registerDomainClasses(EmbeddedHolder, EmbeddedWithMany, EmbeddedMember) + } + + void "the collection of an embedded entity is saved and read back with its owner"() { + given: + EmbeddedHolder holder = new EmbeddedHolder(embed: new EmbeddedWithMany()) + holder.embed.addToMembers(new EmbeddedMember(name: 'a')) + holder.save(flush: true, failOnError: true) + manager.session.clear() + + expect: + EmbeddedHolder.get(holder.id).embed.members*.name == ['a'] + } +} + +@Entity +class EmbeddedHolder { + EmbeddedWithMany embed + + static embedded = ['embed'] +} + +@Entity +class EmbeddedWithMany { + static hasMany = [members: EmbeddedMember] +} + +@Entity +class EmbeddedMember { + String name +}
