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 32d3c5c3147a2630eaa110183c79ecb65777a6a5 Author: Walter Duque de Estrada <[email protected]> AuthorDate: Thu Mar 5 19:28:54 2026 -0600 hibernate7: cleanup ListSecondPassBinder --- .../hibernate/HibernateAssociation.java | 4 ++ .../hibernate/HibernateManyToOneProperty.java | 5 ++ .../hibernate/HibernateOneToManyProperty.java | 4 ++ .../secondpass/ListSecondPassBinder.java | 12 ++--- .../HibernateManyToOnePropertySpec.groovy | 57 ++++++++++++++++++++++ .../HibernateOneToManyPropertySpec.groovy | 56 +++++++++++++++++++++ 6 files changed, 132 insertions(+), 6 deletions(-) diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateAssociation.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateAssociation.java index d5cb2885fc..313315066e 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateAssociation.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateAssociation.java @@ -72,6 +72,10 @@ public interface HibernateAssociation extends HibernatePersistentProperty { return (GrailsHibernatePersistentEntity) getAssociatedEntity(); } + default String getReferencedEntityName() { + return getHibernateAssociatedEntity().getName(); + } + @Override default void validateAssociation() { if (getUserType() != null) { diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateManyToOneProperty.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateManyToOneProperty.java index 97cfb69171..8c50511729 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateManyToOneProperty.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateManyToOneProperty.java @@ -40,7 +40,12 @@ public class HibernateManyToOneProperty extends ManyToOneWithMapping<PropertyCon + public String getReferencedEntityName() { + return getHibernateAssociatedEntity().getName(); + } + public boolean isValidHibernateManyToOne() { + validateAssociation(); return true; } diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateOneToManyProperty.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateOneToManyProperty.java index ec0f6e6338..74400d0dfe 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateOneToManyProperty.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateOneToManyProperty.java @@ -36,4 +36,8 @@ public class HibernateOneToManyProperty extends OneToManyWithMapping<PropertyCon public GrailsHibernatePersistentEntity getHibernateAssociatedEntity() { return (GrailsHibernatePersistentEntity) super.getAssociatedEntity(); } + + public String getReferencedEntityName() { + return getHibernateAssociatedEntity().getName(); + } } diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinder.java index 4fd1d67c30..ed4dfa0862 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinder.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/secondpass/ListSecondPassBinder.java @@ -25,7 +25,9 @@ import java.util.Map; import org.grails.datastore.mapping.model.PersistentEntity; import org.grails.orm.hibernate.cfg.PersistentEntityNamingStrategy; import org.grails.orm.hibernate.cfg.domainbinding.binder.SimpleValueColumnBinder; +import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateAssociation; import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToManyProperty; +import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToOneProperty; import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyProperty; import org.grails.orm.hibernate.cfg.domainbinding.util.BackticksRemover; import org.hibernate.MappingException; @@ -36,8 +38,6 @@ import org.hibernate.mapping.BasicValue; import org.hibernate.mapping.DependantValue; import org.hibernate.mapping.IndexBackref; import org.hibernate.mapping.List; -import org.hibernate.mapping.ManyToOne; -import org.hibernate.mapping.OneToMany; import org.hibernate.mapping.PersistentClass; import org.hibernate.mapping.Table; import org.hibernate.mapping.Value; @@ -95,11 +95,11 @@ public class ListSecondPassBinder { if (property.isBidirectional()) { String entityName; - Value element = list.getElement(); - if (element instanceof ManyToOne manyToOne) { - entityName = manyToOne.getReferencedEntityName(); + HibernateAssociation inverseSide = property.getHibernateInverseSide(); + if (inverseSide instanceof HibernateManyToOneProperty manyToOne) { + entityName = manyToOne.getReferencedEntityName(); } else { - entityName = ((OneToMany) element).getReferencedEntityName(); + entityName = inverseSide.getReferencedEntityName(); } PersistentClass referenced = mappings.getEntityBinding(entityName); diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateManyToOnePropertySpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateManyToOnePropertySpec.groovy new file mode 100644 index 0000000000..3fe6bafc3f --- /dev/null +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateManyToOnePropertySpec.groovy @@ -0,0 +1,57 @@ +/* + * 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 org.grails.orm.hibernate.cfg.domainbinding.hibernate + +import grails.gorm.annotation.Entity +import grails.gorm.specs.HibernateGormDatastoreSpec +import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToOneProperty + +class HibernateManyToOnePropertySpec extends HibernateGormDatastoreSpec { + + def setupSpec() { + manager.addAllDomainClasses([HMTOPBook, HMTOPAuthor]) + } + + void "test getReferencedEntityName returns the correct entity name"() { + given: + def bookEntity = mappingContext.getPersistentEntity(HMTOPBook.name) + HibernateManyToOneProperty property = (HibernateManyToOneProperty) bookEntity.getPropertyByName("author") + + when: + String entityName = property.getReferencedEntityName() + + then: + entityName == HMTOPAuthor.name + } +} + +@Entity +class HMTOPBook { + Long id + String title + HMTOPAuthor author +} + +@Entity +class HMTOPAuthor { + Long id + String name + Set<HMTOPBook> books + static hasMany = [books: HMTOPBook] +} diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateOneToManyPropertySpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateOneToManyPropertySpec.groovy new file mode 100644 index 0000000000..8884fefdbd --- /dev/null +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateOneToManyPropertySpec.groovy @@ -0,0 +1,56 @@ +/* + * 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 org.grails.orm.hibernate.cfg.domainbinding.hibernate + +import grails.gorm.annotation.Entity +import grails.gorm.specs.HibernateGormDatastoreSpec +import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateOneToManyProperty + +class HibernateOneToManyPropertySpec extends HibernateGormDatastoreSpec { + + def setupSpec() { + manager.addAllDomainClasses([HOTMPBook, HOTMPAuthor]) + } + + void "test getReferencedEntityName returns the correct entity name"() { + given: + def authorEntity = mappingContext.getPersistentEntity(HOTMPAuthor.name) + HibernateOneToManyProperty property = (HibernateOneToManyProperty) authorEntity.getPropertyByName("books") + + when: + String entityName = property.getReferencedEntityName() + + then: + entityName == HOTMPBook.name + } +} + +@Entity +class HOTMPBook { + Long id + String title +} + +@Entity +class HOTMPAuthor { + Long id + String name + Set<HOTMPBook> books + static hasMany = [books: HOTMPBook] +}
