This is an automated email from the ASF dual-hosted git repository. borinquenkid pushed a commit to branch 8.0.x-hibernate7 in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 1cec71e06bcb16c71fe35c2418418241565f72b4 Author: Walter Duque de Estrada <[email protected]> AuthorDate: Wed Feb 11 09:08:17 2026 -0600 refactoring ColumnNameForPropertyAndPathFetcher --- .../cfg/GrailsHibernatePersistentProperty.java | 21 +++++++- .../ColumnNameForPropertyAndPathFetcher.java | 58 +++++----------------- .../ColumnNameForPropertyAndPathFetcherSpec.groovy | 56 ++++----------------- 3 files changed, 41 insertions(+), 94 deletions(-) diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsHibernatePersistentProperty.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsHibernatePersistentProperty.java index e561c12fed..a66dde9c9e 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsHibernatePersistentProperty.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsHibernatePersistentProperty.java @@ -4,7 +4,6 @@ import org.grails.datastore.mapping.model.PersistentProperty; import org.grails.datastore.mapping.model.types.Association; import org.grails.datastore.mapping.model.types.Embedded; -import java.util.Objects; import java.util.Optional; import org.hibernate.MappingException; @@ -136,5 +135,25 @@ public interface GrailsHibernatePersistentProperty extends PersistentProperty<Pr .orElseGet(() -> namingStrategy.resolveColumnName(getName()) + GrailsDomainBinder.UNDERSCORE + IndexedCollection.DEFAULT_ELEMENT_COLUMN_NAME); } + default boolean isJoinKeyMapped() { + return getMappedForm() != null && getMappedForm().hasJoinKeyMapping() && supportsJoinColumnMapping(); + } + + default String getMappedColumnName() { + if( getMappedForm() != null) { + return getMappedForm().getColumn(); + } + return null; + } + + default String getColumnName(ColumnConfig cc) { + return Optional.of(this) + .filter(GrailsHibernatePersistentProperty::isJoinKeyMapped) + .map(p -> p.getMappedForm().getJoinTable().getKey().getName()) + .orElseGet(() -> Optional.ofNullable(cc) + .map(ColumnConfig::getName) + .orElseGet(this::getMappedColumnName)); + } + } \ No newline at end of file diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/ColumnNameForPropertyAndPathFetcher.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/ColumnNameForPropertyAndPathFetcher.java index 1114e58797..3407ca98e0 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/ColumnNameForPropertyAndPathFetcher.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/ColumnNameForPropertyAndPathFetcher.java @@ -7,6 +7,8 @@ import org.grails.orm.hibernate.cfg.GrailsHibernateUtil; import org.grails.orm.hibernate.cfg.PersistentEntityNamingStrategy; import org.grails.orm.hibernate.cfg.PropertyConfig; +import java.util.Optional; + public class ColumnNameForPropertyAndPathFetcher { private final PersistentEntityNamingStrategy namingStrategy; @@ -33,51 +35,15 @@ public class ColumnNameForPropertyAndPathFetcher { private static final String UNDERSCORE = "_"; public String getColumnNameForPropertyAndPath(GrailsHibernatePersistentProperty grailsProp, - String path, ColumnConfig cc) { - // First try the column config. - String columnName = null; - if (cc == null) { - // No column config given, attempt to obtain the property config directly from the property - PropertyConfig c = null; - try { - c = ((GrailsHibernatePersistentProperty) grailsProp).getMappedForm(); - } catch (Exception ignore) { - // If we cannot resolve a PropertyConfig, treat as absent and fall back later - } - - if (grailsProp.supportsJoinColumnMapping() && c != null && c.hasJoinKeyMapping()) { - columnName = c.getJoinTable().getKey().getName(); - } - else if (c != null && c.getColumn() != null) { - columnName = c.getColumn(); - } - } - else { - if (grailsProp.supportsJoinColumnMapping()) { - PropertyConfig pc = ((GrailsHibernatePersistentProperty) grailsProp).getMappedForm(); - if (pc.hasJoinKeyMapping()) { - columnName = pc.getJoinTable().getKey().getName(); - } - else { - columnName = cc.getName(); - } - } - else { - columnName = cc.getName(); - } - } - - if (columnName == null) { - if (GrailsHibernateUtil.isNotEmpty(path)) { - String s1 = namingStrategy.resolveColumnName(path); - - String s2 = defaultColumnNameFetcher.getDefaultColumnName(grailsProp); - columnName = backticksRemover.apply(s1) + UNDERSCORE + backticksRemover.apply(s2); - } else { - - columnName = defaultColumnNameFetcher.getDefaultColumnName(grailsProp); - } - } - return columnName; + String path, + ColumnConfig cc) { + return Optional.ofNullable(grailsProp.getColumnName(cc)) + .orElseGet(() -> { + String suffix = defaultColumnNameFetcher.getDefaultColumnName(grailsProp); + return Optional.ofNullable(path) + .filter(GrailsHibernateUtil::isNotEmpty) + .map(p -> backticksRemover.apply(namingStrategy.resolveColumnName(p)) + UNDERSCORE + backticksRemover.apply(suffix)) + .orElse(suffix); + }); } } diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ColumnNameForPropertyAndPathFetcherSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ColumnNameForPropertyAndPathFetcherSpec.groovy index 2659324c88..762c8b0ed3 100644 --- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ColumnNameForPropertyAndPathFetcherSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/ColumnNameForPropertyAndPathFetcherSpec.groovy @@ -1,12 +1,8 @@ package org.grails.orm.hibernate.cfg.domainbinding -import org.grails.datastore.mapping.model.PersistentEntity -import org.grails.datastore.mapping.model.PersistentProperty import org.grails.orm.hibernate.cfg.ColumnConfig import org.grails.orm.hibernate.cfg.GrailsHibernatePersistentProperty -import org.grails.orm.hibernate.cfg.Mapping import org.grails.orm.hibernate.cfg.PersistentEntityNamingStrategy -import org.grails.orm.hibernate.cfg.PropertyConfig import spock.lang.Specification import spock.lang.Unroll @@ -14,7 +10,7 @@ class ColumnNameForPropertyAndPathFetcherSpec extends Specification { def backticksRemover = new BackticksRemover() - def "when ColumnConfig is null and mapping has explicit column then it is used"() { + def "when grailsProp returns a column name then it is used"() { given: def namingStrategy = Mock(PersistentEntityNamingStrategy) def defaultColumnFetcher = Mock(DefaultColumnNameFetcher) @@ -25,52 +21,19 @@ class ColumnNameForPropertyAndPathFetcherSpec extends Specification { ) def grailsProp = Mock(GrailsHibernatePersistentProperty) - def owner = Mock(PersistentEntity) - def mapping = Mock(Mapping) - def pc = Mock(PropertyConfig) + def cc = Mock(ColumnConfig) - grailsProp.supportsJoinColumnMapping() >> false - grailsProp.getMappedForm() >> pc - pc.getColumn() >> "explicit_col" + grailsProp.getColumnName(cc) >> "explicit_col" when: - def result = fetcher.getColumnNameForPropertyAndPath(grailsProp, null, null) + def result = fetcher.getColumnNameForPropertyAndPath(grailsProp, "somePath", cc) then: result == "explicit_col" } - def "when ColumnConfig provided and join key mapping exists then join key name is used"() { - given: - def namingStrategy = Mock(PersistentEntityNamingStrategy) - def defaultColumnFetcher = Mock(DefaultColumnNameFetcher) - def fetcher = new ColumnNameForPropertyAndPathFetcher( - namingStrategy, - defaultColumnFetcher, - backticksRemover - ) - - def grailsProp = Mock(GrailsHibernatePersistentProperty) - def providedColumn = new ColumnConfig(name: "ignored_when_join_key") - def pc = Mock(PropertyConfig) - def joinTable = Mock(org.grails.orm.hibernate.cfg.JoinTable) - def key = new ColumnConfig(name: "join_key_name") - - grailsProp.supportsJoinColumnMapping() >> true - grailsProp.getMappedForm() >> pc - pc.hasJoinKeyMapping() >> true - pc.getJoinTable() >> joinTable - joinTable.getKey() >> key - - when: - def result = fetcher.getColumnNameForPropertyAndPath(grailsProp, null, providedColumn) - - then: - result == "join_key_name" - } - @Unroll - def "when no explicit column then builds from path '#path' and default column '#defaultCol' with backticks removed"() { + def "when grailsProp returns null then builds from path '#path' and default column '#defaultCol' with backticks removed"() { given: def namingStrategy = Mock(PersistentEntityNamingStrategy) def defaultColumnFetcher = Mock(DefaultColumnNameFetcher) @@ -82,9 +45,7 @@ class ColumnNameForPropertyAndPathFetcherSpec extends Specification { def grailsProp = Mock(GrailsHibernatePersistentProperty) - // No config available and no join mapping path - grailsProp.supportsJoinColumnMapping() >> false - + grailsProp.getColumnName(null) >> null namingStrategy.resolveColumnName(path) >> resolvedPath defaultColumnFetcher.getDefaultColumnName(grailsProp) >> defaultCol @@ -100,7 +61,7 @@ class ColumnNameForPropertyAndPathFetcherSpec extends Specification { "invoice" | "invoice" | "line_item_id" || "invoice_line_item_id" } - def "when path is empty falls back to default column name only"() { + def "when grailsProp returns null and path is empty falls back to default column name only"() { given: def namingStrategy = Mock(PersistentEntityNamingStrategy) def defaultColumnFetcher = Mock(DefaultColumnNameFetcher) @@ -112,6 +73,7 @@ class ColumnNameForPropertyAndPathFetcherSpec extends Specification { def grailsProp = Mock(GrailsHibernatePersistentProperty) + grailsProp.getColumnName(null) >> null defaultColumnFetcher.getDefaultColumnName(grailsProp) >> "only_default" when: @@ -120,4 +82,4 @@ class ColumnNameForPropertyAndPathFetcherSpec extends Specification { then: result == "only_default" } -} +} \ No newline at end of file
