You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@grails.apache.org by jd...@apache.org on 2026/10/02 13:56:07 UTC

(grails-core) branch fix/h7-global-user-type-class created (now c6ef10e3bf)

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

jdaugherty pushed a change to branch fix/h7-global-user-type-class
in repository https://gitbox.apache.org/repos/asf/grails-core.git


      at c6ef10e3bf Keep a Class user-type in the Hibernate 7 mapping as a Class

This branch includes the following new commits:

     new c6ef10e3bf Keep a Class user-type in the Hibernate 7 mapping as a Class

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(grails-core) 01/01: Keep a Class user-type in the Hibernate 7 mapping as a Class

Posted by jd...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jdaugherty pushed a commit to branch fix/h7-global-user-type-class
in repository https://gitbox.apache.org/repos/asf/grails-core.git

commit c6ef10e3bfce960102cc5b705f2cc8570dfea97c
Author: James Daugherty <jd...@jdresources.net>
AuthorDate: Fri Oct 2 09:55:53 2026 -0400

    Keep a Class user-type in the Hibernate 7 mapping as a Class
    
    A 'user-type' registered with a Class value, for example in
    grails.gorm.default.mapping:
    
        'user-type'(type: MyUserType, class: Boolean)
    
    failed at startup with "Could not resolve named type: class MyUserType",
    because HibernateMappingBuilder stored args.type.toString(), and
    Class.toString() prefixes the name with "class ". Store the value as
    given, as Hibernate 5 does; Mapping.getTypeName already resolves a Class
    to its name.
---
 .../hibernate/HibernateMappingBuilder.groovy       |   2 +-
 .../mapping/HibernateMappingBuilderSpec.groovy     |   9 ++
 .../orm/hibernate/GlobalUserTypeMappingSpec.groovy | 137 +++++++++++++++++++++
 3 files changed, 147 insertions(+), 1 deletion(-)

diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateMappingBuilder.groovy b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateMappingBuilder.groovy
index 19d8205deb..d5b72e0721 100644
--- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateMappingBuilder.groovy
+++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/hibernate/HibernateMappingBuilder.groovy
@@ -119,7 +119,7 @@ class HibernateMappingBuilder implements MappingConfigurationBuilder<Mapping, Pr
 
     void hibernateCustomUserType(Map<String, Object> args) {
         if (args.type && (args['class'] instanceof Class)) {
-            mapping.userTypes[(Class) args['class']] = args.type.toString()
+            mapping.userTypes[(Class) args['class']] = args.type
         }
     }
 
diff --git a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/hibernate/mapping/HibernateMappingBuilderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/hibernate/mapping/HibernateMappingBuilderSpec.groovy
index 6e36f120bb..bb99d2511f 100644
--- a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/hibernate/mapping/HibernateMappingBuilderSpec.groovy
+++ b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/hibernate/mapping/HibernateMappingBuilderSpec.groovy
@@ -522,6 +522,15 @@ class HibernateMappingBuilderSpec extends Specification {
 
         then:
         m.userTypes[String] == 'myType'
+        m.getTypeName(String) == 'myType'
+    }
+
+    def "hibernateCustomUserType resolves a Class type to its class name"() {
+        when:
+        Mapping m = evaluate { 'user-type'(type: StringBuilder, 'class': String) }
+
+        then:
+        m.getTypeName(String) == StringBuilder.name
     }
 
     def "hibernateCustomUserType is a no-op when class is not a Class"() {
diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/GlobalUserTypeMappingSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/GlobalUserTypeMappingSpec.groovy
new file mode 100644
index 0000000000..7be124156b
--- /dev/null
+++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/GlobalUserTypeMappingSpec.groovy
@@ -0,0 +1,137 @@
+/*
+ *  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
+
+import java.sql.PreparedStatement
+import java.sql.ResultSet
+import java.sql.SQLException
+import java.sql.Types
+
+import groovy.transform.CompileStatic
+
+import grails.gorm.annotation.Entity
+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.descriptor.WrapperOptions
+import org.hibernate.usertype.UserType
+import spock.lang.Specification
+import spock.lang.Unroll
+
+class GlobalUserTypeMappingSpec extends Specification {
+
+    @Unroll
+    void "a global user-type given as #description applies to every property of the mapped class"() {
+        given: 'a datastore whose default mapping registers a user type for Boolean'
+        HibernateDatastore datastore = new HibernateDatastore(DatastoreUtils.createPropertyResolver(
+                (Settings.SETTING_DB_CREATE): 'create-drop',
+                'dataSource.url': "jdbc:h2:mem:globalUserType${iteration};LOCK_TIMEOUT=10000",
+                'grails.gorm.default.mapping': {
+                    'user-type'(type: userType, 'class': Boolean)
+                }
+        ), GlobalUserTypeFlag)
+
+        when: 'an entity with a Boolean property is saved and read back'
+        GlobalUserTypeFlag.withTransaction {
+            new GlobalUserTypeFlag(name: 'enabled', active: true).save(flush: true, failOnError: true)
+            new GlobalUserTypeFlag(name: 'disabled', active: false).save(flush: true, failOnError: true)
+        }
+        Map<String, Boolean> readBack = GlobalUserTypeFlag.withTransaction {
+            GlobalUserTypeFlag.list().collectEntries { [(it.name): it.active] }
+        } as Map<String, Boolean>
+
+        then: 'the property is bound to the user type'
+        def value = datastore.metadata.getEntityBinding(GlobalUserTypeFlag.name).getProperty('active').value as SimpleValue
+        value.typeName == YesNoBooleanUserType.name
+
+        and: 'the user type writes the column'
+        storedFlags(datastore) == [disabled: 'N', enabled: 'Y']
+
+        and: 'the user type reads the column'
+        readBack == [disabled: false, enabled: true]
+
+        cleanup:
+        datastore?.close()
+
+        where:
+        description     | iteration | userType
+        'a Class'       | 1         | YesNoBooleanUserType
+        'a class name'  | 2         | YesNoBooleanUserType.name
+    }
+
+    private static Map<String, String> storedFlags(HibernateDatastore datastore) {
+        Map<String, String> flags = [:]
+        datastore.dataSource.connection.withCloseable { conn ->
+            conn.createStatement().withCloseable { stmt ->
+                stmt.executeQuery('select name, active from global_user_type_flag order by name').withCloseable { rs ->
+                    while (rs.next()) {
+                        flags[rs.getString('name')] = rs.getString('active')
+                    }
+                }
+            }
+        }
+        flags
+    }
+}
+
+@Entity
+class GlobalUserTypeFlag implements HibernateEntity<GlobalUserTypeFlag> {
+    String name
+    Boolean active
+}
+
+@CompileStatic
+class YesNoBooleanUserType implements UserType<Boolean> {
+
+    @Override
+    int getSqlType() {
+        Types.VARCHAR
+    }
+
+    @Override
+    Class<Boolean> returnedClass() {
+        Boolean
+    }
+
+    @Override
+    Boolean nullSafeGet(ResultSet rs, int position, WrapperOptions options) throws SQLException {
+        String value = rs.getString(position)
+        value == null ? null : value == 'Y'
+    }
+
+    @Override
+    void nullSafeSet(PreparedStatement st, Boolean value, int index, WrapperOptions options) throws SQLException {
+        if (value == null) {
+            st.setNull(index, Types.VARCHAR)
+        } else {
+            st.setString(index, value ? 'Y' : 'N')
+        }
+    }
+
+    @Override
+    Boolean deepCopy(Boolean value) {
+        value
+    }
+
+    @Override
+    boolean isMutable() {
+        false
+    }
+}