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 24f733024804dfcacce2077f48f33a33fa4bdb25 Author: Walter Duque de Estrada <[email protected]> AuthorDate: Sat Feb 21 19:13:24 2026 -0600 Upgraded IdentityEnumType for Hibernate 7 --- .../grails/orm/hibernate/cfg/IdentityEnumType.java | 56 ++++++---------- .../grails/gorm/specs/IdentityEnumTypeSpec.groovy | 75 +--------------------- 2 files changed, 23 insertions(+), 108 deletions(-) diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/IdentityEnumType.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/IdentityEnumType.java index 15acfd0d82..8b081a3670 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/IdentityEnumType.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/IdentityEnumType.java @@ -19,19 +19,20 @@ package org.grails.orm.hibernate.cfg; import jakarta.persistence.AttributeConverter; + +import java.io.Serial; import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.*; +import java.util.Collections; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + import org.hibernate.HibernateException; import org.hibernate.MappingException; -import org.hibernate.dialect.Dialect; -import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.type.AbstractStandardBasicType; -import org.hibernate.type.descriptor.jdbc.JdbcType; import org.hibernate.type.spi.TypeConfiguration; import org.hibernate.usertype.ParameterizedType; import org.hibernate.usertype.UserType; @@ -47,6 +48,7 @@ import org.slf4j.LoggerFactory; */ public class IdentityEnumType implements UserType, ParameterizedType, Serializable { + @Serial private static final long serialVersionUID = -6625622185856547501L; private static final Logger LOG = LoggerFactory.getLogger(IdentityEnumType.class); @@ -102,7 +104,7 @@ public class IdentityEnumType implements UserType, ParameterizedType, Serializab } } - public int[] sqlTypes() { + public int[] getSqlTypes() { return sqlTypes; } @@ -115,32 +117,17 @@ public class IdentityEnumType implements UserType, ParameterizedType, Serializab return enumClass; } + @Override public boolean equals(Object o1, Object o2) throws HibernateException { return o1 == o2; } + @Override public int hashCode(Object o) throws HibernateException { return o.hashCode(); } - @Override - public Object nullSafeGet( - ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) - throws SQLException { - Object result = rs.getObject(position, returnedClass()); - return rs.wasNull() ? null : result; - } - @Override - public void nullSafeSet( - PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) - throws HibernateException, SQLException { - if (value == null) { - st.setNull(index, sqlTypes[0]); - } else { - type.nullSafeSet(st, bidiMap.getKey(value), index, session); - } - } public Object deepCopy(Object o) throws HibernateException { return o; @@ -162,19 +149,17 @@ public class IdentityEnumType implements UserType, ParameterizedType, Serializab return orig; } - @Override - public long getDefaultSqlLength(Dialect dialect, JdbcType jdbcType) { - return UserType.super.getDefaultSqlLength(dialect, jdbcType); + + public long getDefaultSqlLength() { + return UserType.super.getDefaultSqlLength(); } - @Override - public int getDefaultSqlPrecision(Dialect dialect, JdbcType jdbcType) { - return UserType.super.getDefaultSqlPrecision(dialect, jdbcType); + public int getDefaultSqlPrecision() { + return UserType.super.getDefaultSqlPrecision(); } - @Override - public int getDefaultSqlScale(Dialect dialect, JdbcType jdbcType) { - return UserType.super.getDefaultSqlScale(dialect, jdbcType); + public int getDefaultSqlScale() { + return UserType.super.getDefaultSqlScale(); } @Override @@ -185,10 +170,11 @@ public class IdentityEnumType implements UserType, ParameterizedType, Serializab @SuppressWarnings({"rawtypes", "unchecked"}) private static class BidiEnumMap implements Serializable { + @Serial private static final long serialVersionUID = 3325751131102095834L; private final Map enumToKey; private final Map keytoEnum; - private Class keyType; + private final Class keyType; private BidiEnumMap(Class<? extends Enum> enumClass) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { diff --git a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/specs/IdentityEnumTypeSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/specs/IdentityEnumTypeSpec.groovy index f86a750e02..033a610829 100644 --- a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/specs/IdentityEnumTypeSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/specs/IdentityEnumTypeSpec.groovy @@ -24,12 +24,9 @@ import jakarta.persistence.Enumerated import jakarta.persistence.EnumType import org.grails.orm.hibernate.cfg.IdentityEnumType import org.hibernate.MappingException -import org.hibernate.engine.spi.SharedSessionContractImplementor import javax.sql.DataSource -import java.sql.PreparedStatement import java.sql.ResultSet -import java.sql.Types /** * Created by graemerocher on 16/11/16. @@ -79,8 +76,8 @@ class IdentityEnumTypeSpec extends HibernateGormDatastoreSpec { then: type.returnedClass() == IdentityStatusEnum - type.sqlTypes() != null - type.sqlTypes().length > 0 + type.getSqlTypes() != null + type.getSqlTypes().length > 0 } def "setParameterValues throws MappingException for enum without getId method"() { @@ -164,74 +161,6 @@ class IdentityEnumTypeSpec extends HibernateGormDatastoreSpec { type.replace(IdentityStatusEnum.ACTIVE, IdentityStatusEnum.INACTIVE, null).is(IdentityStatusEnum.ACTIVE) } - def "nullSafeSet with null value sets SQL null on PreparedStatement"() { - given: - def type = new IdentityEnumType() - def props = new Properties() - props.setProperty(IdentityEnumType.PARAM_ENUM_CLASS, IdentityStatusEnum.name) - type.setParameterValues(props) - def stmt = Mock(PreparedStatement) - def session = Mock(SharedSessionContractImplementor) - - when: - type.nullSafeSet(stmt, null, 1, session) - - then: - 1 * stmt.setNull(1, type.sqlTypes()[0]) - } - - def "nullSafeSet with non-null value writes the enum id to PreparedStatement"() { - given: - def type = new IdentityEnumType() - def props = new Properties() - props.setProperty(IdentityEnumType.PARAM_ENUM_CLASS, IdentityStatusEnum.name) - type.setParameterValues(props) - def stmt = Mock(PreparedStatement) - def session = Mock(SharedSessionContractImplementor) - - when: - type.nullSafeSet(stmt, IdentityStatusEnum.ACTIVE, 1, session) - - then: - // The String id "A" is written via setString - 1 * stmt.setString(1, "A") - 0 * stmt.setNull(_, _) - } - - def "nullSafeGet returns null when ResultSet value is null"() { - given: - def type = new IdentityEnumType() - def props = new Properties() - props.setProperty(IdentityEnumType.PARAM_ENUM_CLASS, IdentityStatusEnum.name) - type.setParameterValues(props) - def rs = Mock(ResultSet) - rs.getObject(1, IdentityStatusEnum) >> null - rs.wasNull() >> true - - when: - def result = type.nullSafeGet(rs, 1, Mock(SharedSessionContractImplementor), null) - - then: - result == null - } - - def "nullSafeGet returns the enum value from ResultSet"() { - given: - def type = new IdentityEnumType() - def props = new Properties() - props.setProperty(IdentityEnumType.PARAM_ENUM_CLASS, IdentityStatusEnum.name) - type.setParameterValues(props) - def rs = Mock(ResultSet) - rs.getObject(1, IdentityStatusEnum) >> IdentityStatusEnum.INACTIVE - rs.wasNull() >> false - - when: - def result = type.nullSafeGet(rs, 1, Mock(SharedSessionContractImplementor), null) - - then: - result == IdentityStatusEnum.INACTIVE - } - def "getBidiEnumMap logs warning for duplicate enum ids and still returns a map"() { when: def map = IdentityEnumType.getBidiEnumMap(DuplicateIdEnum)
