This is an automated email from the ASF dual-hosted git repository. borinquenkid pushed a commit to branch 9.0.x-h7-improvement-phase3 in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 6855e32c42ed010bf938e51258d548c3a9cb7f31 Author: Walter Duque de Estrada <[email protected]> AuthorDate: Tue Oct 6 17:17:13 2026 -0500 feat: the generated-class mode states a custom-type property that names a UserType class Upstream's new fixtures (UserTypeComparisonItem, JpaCriteriaQueryCreatorSpecGraded) map a value object of a class GORM does not know with type: SomeUserType. The binder reads such a property as a HibernateCustomProperty and binds it like a simple one from the type its mapping names, but the generator refused it, so 69 specs that boot those entities failed in generated mode. The generator now supports a non-enum custom property whose mapping names a UserType class (@Type, the column and the rest as for a simple property); a custom property with no type mapped is still refused, by name. The generator and DDL differentials now cover the two entities with no mismatch and no refusal entry; a new GrailsDomainGeneratorSpec feature pins the @Type of a value object. UserTypeMappingSpec read SimpleValue.typeName, which Hibernate's annotation binder leaves unset, so it now falls back to the resolved type (the user type class, or the Java class) and runs in both modes. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> --- .../domainbinding/jpa/GrailsDomainGenerator.groovy | 28 ++++-- .../orm/hibernate/UserTypeMappingSpec.groovy | 10 ++- ...eneratedDomainClassesDdlDifferentialSpec.groovy | 4 - .../jpa/GrailsDomainGeneratorSpec.groovy | 99 +++++++++++++++++++++- 4 files changed, 130 insertions(+), 11 deletions(-) diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainGenerator.groovy b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainGenerator.groovy index e782f16a35..484769fd42 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainGenerator.groovy +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainGenerator.groovy @@ -140,6 +140,7 @@ import org.grails.orm.hibernate.cfg.domainbinding.generator.GrailsTableGenerator import org.grails.orm.hibernate.cfg.domainbinding.hibernate.GrailsHibernatePersistentEntity import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateAssociation import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateBasicProperty +import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateCustomProperty import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateEmbeddedCollectionProperty import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateEmbeddedProperty import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateEnumProperty @@ -582,9 +583,14 @@ class GrailsDomainGenerator { return "Association property [${property.name}] of [${entity.name}]: ${problem}" } } - if ((property instanceof HibernateSimpleProperty || property instanceof HibernateTenantIdProperty) && !decideType(property).supported) { + if ((property instanceof HibernateSimpleProperty || property instanceof HibernateTenantIdProperty || + isCustomProperty(property)) && !decideType(property).supported) { return typeNotSupported(property, decideType(property)) } + if (isCustomProperty(property)) { + return "Property [${property.name}] of [${entity.name}] is a HibernateCustomProperty with no type mapped, " + + 'which the generator does not support yet: it states a custom type only when the mapping names a UserType class' + } return "Property [${property.name}] of [${entity.name}] is a ${property.getClass().simpleName}, " + 'which the generator does not support yet' } @@ -925,8 +931,9 @@ class GrailsDomainGenerator { /** * @return whether the generator can describe the property today: a plain single-column basic property, a * derived (formula) property, the tenant id (an ordinary column), the version, an enum, an embedded object whose own properties are all - * supported, a collection of basic values or enums, a many-to-one association, or the simple identifier. Custom types, - * multi-column properties and the other associations are not supported yet. + * supported, a collection of basic values or enums, a many-to-one association, the simple identifier, or a property of a type + * GORM does not know ({@link HibernateCustomProperty}) that the mapping gives a {@code UserType} class. Custom types with no + * type mapped, multi-column properties and the other associations are not supported yet. */ boolean supports(HibernatePersistentProperty property) { if (property instanceof HibernateSimpleIdentityProperty) { @@ -944,7 +951,8 @@ class GrailsDomainGenerator { if (property instanceof HibernateToManyEntityProperty) { return toManyProblem((HibernateToManyEntityProperty) property) == null } - if (!(property instanceof HibernateSimpleProperty) && !(property instanceof HibernateTenantIdProperty)) { + if (!(property instanceof HibernateSimpleProperty) && !(property instanceof HibernateTenantIdProperty) && + !isCustomProperty(property)) { return false } PropertyConfig mappedForm = property.hibernateMappedForm @@ -955,7 +963,17 @@ class GrailsDomainGenerator { // the enum binder never reads the formula, it always binds a column return false } - return decideType(property).supported + TypeDecision decision = decideType(property) + // the binder binds a custom property like a simple one, from the type its mapping names; with none, the type it resolves + // from the class alone is not one an annotation states, so only a mapped UserType class is supported + return decision.supported && (!isCustomProperty(property) || decision.facets != null) + } + + /** + * @return whether the property is of a type GORM does not know and that is not an enum: the binder binds it like a simple property + */ + private static boolean isCustomProperty(HibernatePersistentProperty property) { + return property instanceof HibernateCustomProperty && !(property instanceof HibernateEnumProperty) } /** diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/UserTypeMappingSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/UserTypeMappingSpec.groovy index 8ce74887a8..92b68c1cf8 100644 --- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/UserTypeMappingSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/UserTypeMappingSpec.groovy @@ -30,6 +30,8 @@ import grails.gorm.hibernate.HibernateEntity import org.grails.datastore.mapping.core.DatastoreUtils import org.grails.orm.hibernate.cfg.Settings import org.hibernate.mapping.SimpleValue +import org.hibernate.type.CustomType +import org.hibernate.type.Type import org.hibernate.type.descriptor.WrapperOptions import org.hibernate.usertype.UserType import spock.lang.Specification @@ -128,7 +130,13 @@ class UserTypeMappingSpec extends Specification { } private static String typeNameOf(HibernateDatastore datastore, Class entity) { - (datastore.metadata.getEntityBinding(entity.name).getProperty('active').value as SimpleValue).typeName + SimpleValue value = datastore.metadata.getEntityBinding(entity.name).getProperty('active').value as SimpleValue + // the domain binder states the type by name, Hibernate's annotation binder (hibernate.generatedDomainClasses) by the type it resolves + value.typeName ?: resolvedTypeName(value.type) + } + + private static String resolvedTypeName(Type type) { + type instanceof CustomType ? ((CustomType) type).userType.class.name : type.returnedClass.name } private static Map<String, String> storedFlags(HibernateDatastore datastore, String table) { diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GeneratedDomainClassesDdlDifferentialSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GeneratedDomainClassesDdlDifferentialSpec.groovy index 6b03637b9d..7f68a66914 100644 --- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GeneratedDomainClassesDdlDifferentialSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GeneratedDomainClassesDdlDifferentialSpec.groovy @@ -90,10 +90,6 @@ class GeneratedDomainClassesDdlDifferentialSpec extends Specification { 'which is not a persistent property of the entity', 'is a registered type with type parameters', 'the index column of the map is mapped with the type', - // not stated by an annotation because the generator does not describe a custom-type property yet (a value object mapped - // with type: SomeUserType, UserTypeComparisonItem and JpaCriteriaQueryCreatorSpecGraded in the scanned domains): a gap to - // close by treating it like a simple property with a UserType, not a mapping that cannot be stated - 'is a HibernateCustomProperty, which the generator does not support yet', ] private static final AtomicInteger BOOTS = new AtomicInteger() diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainGeneratorSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainGeneratorSpec.groovy index 2afd5e7778..2d609c3c36 100644 --- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainGeneratorSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/jpa/GrailsDomainGeneratorSpec.groovy @@ -122,6 +122,7 @@ import org.grails.orm.hibernate.cfg.PropertyConfig import org.grails.orm.hibernate.cfg.HibernateMappingContext import org.grails.orm.hibernate.connections.HibernateConnectionSourceSettings import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateBasicProperty +import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateCustomProperty import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateToManyEntityProperty import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateEmbeddedProperty import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateManyToOneProperty @@ -161,7 +162,7 @@ class GrailsDomainGeneratorSpec extends HibernateGormDatastoreSpec { GenEmbedOwner, GenEmbedOther, GenEmbedBase, GenEmbedChild, GenEmbedFormulaOwner, GenCollSingle, GenCollKinds, GenCollLazy, GenFkTarget, GenFkOwned, GenFkOwner, GenFkCascades, GenFkNodeA, GenFkNodeB, GenFkHasOneOwner, GenFkHasOneDetail, GenOneFace, GenOneNose, GenFkManyOne, GenFkOneSide, GenFkSub, GenFkSubRoot, GenOmParent, GenOmChild, GenOmListed, GenOmOrdered, GenOmOrphan, GenOmFetched, GenOmTag, GenOmStep, GenOmKept, GenOmSortedOwner, GenOmMapOwner, GenMapBidiOwner, GenMapBidiChild, GenEmbAssocOwner, - GenMmStudent, GenMmCourse, GenMmPerson, GenParamsOnly, GenDecimal, GenUnversioned, GenUnversionedRoot, GenUnversionedChild, GenConverted) + GenMmStudent, GenMmCourse, GenMmPerson, GenParamsOnly, GenDecimal, GenUnversioned, GenUnversionedRoot, GenUnversionedChild, GenConverted, GenValueTyped) } List<StandardServiceRegistry> registries = [] @@ -398,6 +399,24 @@ class GrailsDomainGeneratorSpec extends HibernateGormDatastoreSpec { return converter.hasProperty('converterBean') ? converter.converterBean.beanClass : converter.getClass() } + void "a value object of a class GORM does not know, mapped with a UserType class, becomes @Type like a simple property"() { + given: + GrailsHibernatePersistentEntity entity = unbound(GenValueTyped) + HibernatePersistentProperty property = entity.getHibernatePropertyByName('amount') + + when: + Class<?> valueTyped = generate(GenValueTyped) + + then: + property instanceof HibernateCustomProperty + newGenerator().supports(property) + newGenerator().typeFacets(property).userType() == GenMoneyType + valueTyped.getDeclaredField('amount').getAnnotation(Type).value() == GenMoneyType + valueTyped.getDeclaredField('amount').type == GenMoney + valueTyped.getDeclaredField('amount').isAnnotationPresent(Column) + !valueTyped.getDeclaredField('name').isAnnotationPresent(Type) + } + void "a property without an explicit type carries no type annotation"() { when: Class<?> typed = generate(GenTyped) @@ -2541,6 +2560,84 @@ class GenTyped { } } +@Entity +class GenValueTyped { + + String name + GenMoney amount + + static mapping = { + amount type: GenMoneyType + } +} + +class GenMoney implements Serializable { + + final long cents + + GenMoney(long cents) { + this.cents = cents + } +} + +class GenMoneyType implements UserType<GenMoney> { + + @Override + int getSqlType() { + return Types.BIGINT + } + + @Override + Class<GenMoney> returnedClass() { + return GenMoney + } + + @Override + boolean equals(GenMoney x, GenMoney y) { + return x?.cents == y?.cents + } + + @Override + int hashCode(GenMoney x) { + return Long.hashCode(x.cents) + } + + @Override + GenMoney nullSafeGet(ResultSet rs, int position, WrapperOptions options) throws SQLException { + long cents = rs.getLong(position) + return rs.wasNull() ? null : new GenMoney(cents) + } + + @Override + void nullSafeSet(PreparedStatement st, GenMoney value, int index, WrapperOptions options) throws SQLException { + if (value == null) { + st.setNull(index, Types.BIGINT) + } else { + st.setLong(index, value.cents) + } + } + + @Override + GenMoney deepCopy(GenMoney value) { + return value + } + + @Override + boolean isMutable() { + return false + } + + @Override + Serializable disassemble(GenMoney value) { + return value + } + + @Override + GenMoney assemble(Serializable cached, Object owner) { + return (GenMoney) cached + } +} + @Entity class GenConverted {
