HHH-16125 clean up legacy EnumType

This commit is contained in:
Gavin 2023-04-28 04:52:18 +02:00 committed by Gavin King
parent febf39cc55
commit d075093ebf
6 changed files with 101 additions and 276 deletions

View File

@ -64,6 +64,7 @@ import jakarta.persistence.AttributeConverter;
import jakarta.persistence.EnumType; import jakarta.persistence.EnumType;
import jakarta.persistence.TemporalType; import jakarta.persistence.TemporalType;
import static java.lang.Boolean.parseBoolean;
import static org.hibernate.mapping.MappingHelper.injectParameters; import static org.hibernate.mapping.MappingHelper.injectParameters;
/** /**
@ -370,9 +371,9 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
protected Resolution<?> buildResolution() { protected Resolution<?> buildResolution() {
Properties typeParameters = getTypeParameters(); Properties typeParameters = getTypeParameters();
if ( typeParameters != null if (typeParameters != null
&& Boolean.parseBoolean( typeParameters.getProperty( DynamicParameterizedType.IS_DYNAMIC ) ) && parseBoolean(typeParameters.getProperty(DynamicParameterizedType.IS_DYNAMIC))
&& typeParameters.get( DynamicParameterizedType.PARAMETER_TYPE ) == null ) { && typeParameters.get(DynamicParameterizedType.PARAMETER_TYPE) == null ) {
createParameterImpl(); createParameterImpl();
} }
if ( explicitTypeName != null ) { if ( explicitTypeName != null ) {
@ -830,10 +831,10 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
} }
if ( typeInstance instanceof DynamicParameterizedType ) { if ( typeInstance instanceof DynamicParameterizedType ) {
if ( Boolean.parseBoolean( properties.getProperty( DynamicParameterizedType.IS_DYNAMIC ) ) ) { if (parseBoolean(properties.getProperty(DynamicParameterizedType.IS_DYNAMIC))) {
if ( properties.get( DynamicParameterizedType.PARAMETER_TYPE ) == null ) { if (properties.get(DynamicParameterizedType.PARAMETER_TYPE) == null) {
final DynamicParameterizedType.ParameterType parameterType = makeParameterImpl(); final DynamicParameterizedType.ParameterType parameterType = makeParameterImpl();
properties.put( DynamicParameterizedType.PARAMETER_TYPE, parameterType ); properties.put(DynamicParameterizedType.PARAMETER_TYPE, parameterType);
} }
} }
} }

View File

@ -45,7 +45,6 @@ import org.hibernate.id.factory.spi.CustomIdGeneratorCreationContext;
import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.ReflectHelper; import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.type.descriptor.converter.spi.JpaAttributeConverter; import org.hibernate.type.descriptor.converter.spi.JpaAttributeConverter;
import org.hibernate.resource.beans.spi.ManagedBeanRegistry; import org.hibernate.resource.beans.spi.ManagedBeanRegistry;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
@ -63,6 +62,7 @@ import org.hibernate.usertype.DynamicParameterizedType;
import jakarta.persistence.AttributeConverter; import jakarta.persistence.AttributeConverter;
import static java.lang.Boolean.parseBoolean;
import static org.hibernate.boot.model.convert.spi.ConverterDescriptor.TYPE_NAME_PREFIX; import static org.hibernate.boot.model.convert.spi.ConverterDescriptor.TYPE_NAME_PREFIX;
import static org.hibernate.id.factory.internal.IdentifierGeneratorUtil.createLegacyIdentifierGenerator; import static org.hibernate.id.factory.internal.IdentifierGeneratorUtil.createLegacyIdentifierGenerator;
import static org.hibernate.internal.util.collections.ArrayHelper.toBooleanArray; import static org.hibernate.internal.util.collections.ArrayHelper.toBooleanArray;
@ -930,7 +930,7 @@ public abstract class SimpleValue implements KeyValue {
final XProperty xProperty = (XProperty) typeParameters.get( DynamicParameterizedType.XPROPERTY ); final XProperty xProperty = (XProperty) typeParameters.get( DynamicParameterizedType.XPROPERTY );
// todo : not sure this works for handling @MapKeyEnumerated // todo : not sure this works for handling @MapKeyEnumerated
final Annotation[] annotations = xProperty == null final Annotation[] annotations = xProperty == null
? null ? new Annotation[0]
: xProperty.getAnnotations(); : xProperty.getAnnotations();
final ClassLoaderService classLoaderService = getMetadata() final ClassLoaderService classLoaderService = getMetadata()
@ -941,13 +941,13 @@ public abstract class SimpleValue implements KeyValue {
DynamicParameterizedType.PARAMETER_TYPE, DynamicParameterizedType.PARAMETER_TYPE,
new ParameterTypeImpl( new ParameterTypeImpl(
classLoaderService.classForTypeName( classLoaderService.classForTypeName(
typeParameters.getProperty( DynamicParameterizedType.RETURNED_CLASS ) typeParameters.getProperty(DynamicParameterizedType.RETURNED_CLASS)
), ),
annotations, annotations,
table.getCatalog(), table.getCatalog(),
table.getSchema(), table.getSchema(),
table.getName(), table.getName(),
Boolean.parseBoolean( typeParameters.getProperty( DynamicParameterizedType.IS_PRIMARY_KEY ) ), parseBoolean(typeParameters.getProperty(DynamicParameterizedType.IS_PRIMARY_KEY)),
columnNames, columnNames,
columnLengths columnLengths
) )
@ -974,7 +974,7 @@ public abstract class SimpleValue implements KeyValue {
final XProperty xProperty = (XProperty) typeParameters.get( DynamicParameterizedType.XPROPERTY ); final XProperty xProperty = (XProperty) typeParameters.get( DynamicParameterizedType.XPROPERTY );
// todo : not sure this works for handling @MapKeyEnumerated // todo : not sure this works for handling @MapKeyEnumerated
final Annotation[] annotations = xProperty == null final Annotation[] annotations = xProperty == null
? null ? new Annotation[0]
: xProperty.getAnnotations(); : xProperty.getAnnotations();
final ClassLoaderService classLoaderService = getMetadata() final ClassLoaderService classLoaderService = getMetadata()
@ -983,12 +983,12 @@ public abstract class SimpleValue implements KeyValue {
.getService( ClassLoaderService.class ); .getService( ClassLoaderService.class );
return new ParameterTypeImpl( return new ParameterTypeImpl(
classLoaderService.classForTypeName( typeParameters.getProperty( DynamicParameterizedType.RETURNED_CLASS ) ), classLoaderService.classForTypeName(typeParameters.getProperty(DynamicParameterizedType.RETURNED_CLASS)),
annotations, annotations,
table.getCatalog(), table.getCatalog(),
table.getSchema(), table.getSchema(),
table.getName(), table.getName(),
Boolean.parseBoolean( typeParameters.getProperty( DynamicParameterizedType.IS_PRIMARY_KEY ) ), parseBoolean(typeParameters.getProperty(DynamicParameterizedType.IS_PRIMARY_KEY)),
columnNames, columnNames,
columnLengths columnLengths
); );

View File

@ -20,7 +20,8 @@ public class JdbcParameterBindingImpl implements JdbcParameterBinding {
public JdbcParameterBindingImpl(JdbcMapping jdbcMapping, Object bindValue) { public JdbcParameterBindingImpl(JdbcMapping jdbcMapping, Object bindValue) {
assert bindValue == null || jdbcMapping == null || jdbcMapping.getJdbcJavaType().isInstance( bindValue ) assert bindValue == null || jdbcMapping == null || jdbcMapping.getJdbcJavaType().isInstance( bindValue )
: String.format( Locale.ROOT, "Unexpected value type (expected : %s) : %s (%s)", jdbcMapping.getJdbcJavaType().getJavaTypeClass().getName(), bindValue, bindValue.getClass().getName() ); : String.format( Locale.ROOT, "Unexpected value type (expected : %s) : %s (%s)",
jdbcMapping.getJdbcJavaType().getJavaTypeClass().getName(), bindValue, bindValue.getClass().getName() );
this.jdbcMapping = jdbcMapping; this.jdbcMapping = jdbcMapping;
this.bindValue = bindValue; this.bindValue = bindValue;

View File

@ -227,14 +227,14 @@ public class SchemaCreatorImpl implements SchemaCreator {
Dialect dialect, Dialect dialect,
Formatter formatter, Formatter formatter,
GenerationTarget... targets) { GenerationTarget... targets) {
final SqlStringGenerationContext context = createSqlStringGenerationContext( options, metadata ); final SqlStringGenerationContext context = createSqlStringGenerationContext(options, metadata);
final Set<String> exportIdentifiers = setOfSize( 50 ); final Set<String> exportIdentifiers = setOfSize(50);
createSchemasAndCatalogs( metadata, options, dialect, formatter, context, targets ); createSchemasAndCatalogs(metadata, options, dialect, formatter, context, targets);
// next, create all UDTs
createUserDefinedTypes( metadata, options, dialect, formatter, context, targets );
// next, create all "before table" auxiliary objects // next, create all "before table" auxiliary objects
createAuxiliaryObjectsBeforeTables( metadata, options, dialect, formatter, context, exportIdentifiers, targets ); createAuxiliaryObjectsBeforeTables(metadata, options, dialect, formatter, context, exportIdentifiers, targets);
// next, create all UDTs
createUserDefinedTypes(metadata, options, dialect, formatter, context, targets);
// then, create all schema objects (tables, sequences, constraints, etc) in each schema // then, create all schema objects (tables, sequences, constraints, etc) in each schema
createSequencesTablesConstraints( createSequencesTablesConstraints(
metadata, metadata,

View File

@ -11,7 +11,6 @@ import java.lang.annotation.Annotation;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Types;
import java.util.Properties; import java.util.Properties;
import jakarta.persistence.Enumerated; import jakarta.persistence.Enumerated;
import jakarta.persistence.MapKeyEnumerated; import jakarta.persistence.MapKeyEnumerated;
@ -24,13 +23,7 @@ import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.util.ReflectHelper; import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.type.descriptor.converter.internal.NamedEnumValueConverter;
import org.hibernate.type.descriptor.converter.internal.OrdinalEnumValueConverter;
import org.hibernate.type.descriptor.converter.spi.BasicValueConverter; import org.hibernate.type.descriptor.converter.spi.BasicValueConverter;
import org.hibernate.type.descriptor.converter.spi.EnumValueConverter;
import org.hibernate.type.descriptor.ValueBinder;
import org.hibernate.type.descriptor.ValueExtractor;
import org.hibernate.type.descriptor.java.EnumJavaType; import org.hibernate.type.descriptor.java.EnumJavaType;
import org.hibernate.type.descriptor.java.JavaType; import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.jdbc.JdbcType; import org.hibernate.type.descriptor.jdbc.JdbcType;
@ -43,6 +36,10 @@ import org.hibernate.usertype.LoggableUserType;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import static jakarta.persistence.EnumType.ORDINAL;
import static jakarta.persistence.EnumType.STRING;
import static org.hibernate.internal.util.config.ConfigurationHelper.getBoolean;
/** /**
* Value type mapper for enumerations. * Value type mapper for enumerations.
* *
@ -50,7 +47,7 @@ import org.jboss.logging.Logger;
* @author Hardy Ferentschik * @author Hardy Ferentschik
* @author Steve Ebersole * @author Steve Ebersole
* *
* @deprecated Use {@link ConvertedBasicType} instead * @deprecated Use the built-in support for enums
*/ */
@Deprecated(since="6.2", forRemoval=true) @Deprecated(since="6.2", forRemoval=true)
public class EnumType<T extends Enum<T>> public class EnumType<T extends Enum<T>>
@ -63,34 +60,14 @@ public class EnumType<T extends Enum<T>>
private Class<T> enumClass; private Class<T> enumClass;
private EnumValueConverter<T, Object> enumValueConverter;
private JdbcType jdbcType; private JdbcType jdbcType;
private ValueExtractor<Object> jdbcValueExtractor; private EnumJavaType<T> enumJavaType;
private ValueBinder<Object> jdbcValueBinder;
private TypeConfiguration typeConfiguration; private TypeConfiguration typeConfiguration;
public EnumType() { public EnumType() {
} }
@SuppressWarnings("unchecked")
public EnumType(
Class<T> enumClass,
EnumValueConverter<T,?> enumValueConverter,
TypeConfiguration typeConfiguration) {
this.enumClass = enumClass;
this.typeConfiguration = typeConfiguration;
this.enumValueConverter = (EnumValueConverter<T,Object>) enumValueConverter;
this.jdbcType = typeConfiguration.getJdbcTypeRegistry().getDescriptor( enumValueConverter.getJdbcTypeCode() );
this.jdbcValueExtractor = (ValueExtractor<Object>) jdbcType.getExtractor( enumValueConverter.getRelationalJavaType() );
this.jdbcValueBinder = (ValueBinder<Object>) jdbcType.getBinder( enumValueConverter.getRelationalJavaType() );
}
public EnumValueConverter<T, ?> getEnumValueConverter() {
return enumValueConverter;
}
@Override @Override
public JdbcType getJdbcType(TypeConfiguration typeConfiguration) { public JdbcType getJdbcType(TypeConfiguration typeConfiguration) {
return jdbcType; return jdbcType;
@ -98,7 +75,7 @@ public class EnumType<T extends Enum<T>>
@Override @Override
public BasicValueConverter<T, Object> getValueConverter() { public BasicValueConverter<T, Object> getValueConverter() {
return enumValueConverter; return null;
} }
/** /**
@ -137,15 +114,49 @@ public class EnumType<T extends Enum<T>>
// 2) we are not passed a ParameterType - generally this indicates a hbm.xml binding case. // 2) we are not passed a ParameterType - generally this indicates a hbm.xml binding case.
final ParameterType reader = (ParameterType) parameters.get( PARAMETER_TYPE ); final ParameterType reader = (ParameterType) parameters.get( PARAMETER_TYPE );
// the `reader != null` block handles annotations, while the `else` block handles hbm.xml if ( parameters.containsKey( ENUM ) ) {
if ( reader != null ) { final String enumClassName = (String) parameters.get( ENUM );
configureUsingReader( reader ); try {
enumClass = ReflectHelper.classForName( enumClassName, this.getClass() ).asSubclass( Enum.class );
}
catch ( ClassNotFoundException exception ) {
throw new HibernateException("Enum class not found: " + enumClassName, exception);
}
}
else if ( reader != null ) {
enumClass = (Class<T>) reader.getReturnedClass().asSubclass( Enum.class );
} }
else { else {
configureUsingParameters( parameters ); throw new AssertionFailure( "No enum class" );
}
final JavaType<T> descriptor = typeConfiguration.getJavaTypeRegistry().getDescriptor( enumClass );
enumJavaType = (EnumJavaType<T>) descriptor;
if ( parameters.containsKey( TYPE ) ) {
int jdbcTypeCode = Integer.parseInt( (String) parameters.get( TYPE ) );
jdbcType = typeConfiguration.getJdbcTypeRegistry().getDescriptor( jdbcTypeCode );
}
else {
final LocalJdbcTypeIndicators indicators;
final Long columnLength = reader == null ? null : reader.getColumnLengths()[0];
if ( parameters.containsKey (NAMED ) ) {
indicators = new LocalJdbcTypeIndicators(
// use ORDINAL as default for hbm.xml mappings
getBoolean( NAMED, parameters, false ) ? STRING : ORDINAL,
false,
columnLength
);
}
else {
indicators = new LocalJdbcTypeIndicators(
getEnumType( reader ),
isNationalized( reader ),
columnLength
);
}
jdbcType = descriptor.getRecommendedJdbcType( indicators );
} }
jdbcValueExtractor = jdbcType.getExtractor( enumValueConverter.getRelationalJavaType() );
jdbcValueBinder = jdbcType.getBinder( enumValueConverter.getRelationalJavaType() );
if ( LOG.isDebugEnabled() ) { if ( LOG.isDebugEnabled() ) {
LOG.debugf( LOG.debugf(
@ -156,74 +167,25 @@ public class EnumType<T extends Enum<T>>
} }
} }
@SuppressWarnings("unchecked")
private void configureUsingParameters(Properties parameters) {
final String enumClassName = (String) parameters.get( ENUM );
try {
enumClass = ReflectHelper.classForName( enumClassName, this.getClass() ).asSubclass( Enum.class );
}
catch ( ClassNotFoundException exception ) {
throw new HibernateException( "Enum class not found: " + enumClassName, exception );
}
enumValueConverter = (EnumValueConverter<T,Object>) interpretParameters( parameters ); //this typecast is rubbish
jdbcType = typeConfiguration.getJdbcTypeRegistry().getDescriptor( enumValueConverter.getJdbcTypeCode() );
}
@SuppressWarnings({"rawtypes","unchecked"})
private void configureUsingReader(ParameterType reader) {
enumClass = (Class<T>) reader.getReturnedClass().asSubclass( Enum.class );
final jakarta.persistence.EnumType enumType = getEnumType( reader );
final JavaType<T> descriptor = typeConfiguration.getJavaTypeRegistry().getDescriptor( enumClass );
final EnumJavaType<T> enumJavaType = (EnumJavaType<T>) descriptor;
final LocalJdbcTypeIndicators indicators = new LocalJdbcTypeIndicators( enumType, reader );
final JavaType<?> relationalJavaType = resolveRelationalJavaType( indicators, enumJavaType );
jdbcType = relationalJavaType.getRecommendedJdbcType( indicators );
enumValueConverter = isOrdinal( enumType )
? new OrdinalEnumValueConverter( enumJavaType, jdbcType, relationalJavaType )
: new NamedEnumValueConverter( enumJavaType, jdbcType, relationalJavaType );
}
private static boolean isOrdinal(jakarta.persistence.EnumType enumType) {
if ( enumType == null ) {
return true;
}
else {
switch ( enumType ) {
case ORDINAL:
return true;
case STRING:
return false;
default:
throw new AssertionFailure( "Unknown EnumType: " + enumType);
}
}
}
private JavaType<? extends Number> resolveRelationalJavaType(
LocalJdbcTypeIndicators indicators,
EnumJavaType<?> enumJavaType) {
return enumJavaType.getRecommendedJdbcType( indicators )
.getJdbcRecommendedJavaTypeMapping( null, null, typeConfiguration );
}
private jakarta.persistence.EnumType getEnumType(ParameterType reader) { private jakarta.persistence.EnumType getEnumType(ParameterType reader) {
if ( reader == null ) { if ( reader != null ) {
return null; if ( reader.isPrimaryKey() ) {
} final MapKeyEnumerated enumAnn = getAnnotation( reader.getAnnotationsMethod(), MapKeyEnumerated.class );
if ( enumAnn != null ) {
if ( reader.isPrimaryKey() ) { return enumAnn.value();
final MapKeyEnumerated enumAnn = getAnnotation( reader.getAnnotationsMethod(), MapKeyEnumerated.class ); }
}
final Enumerated enumAnn = getAnnotation( reader.getAnnotationsMethod(), Enumerated.class );
if ( enumAnn != null ) { if ( enumAnn != null ) {
return enumAnn.value(); return enumAnn.value();
} }
} }
return ORDINAL;
}
final Enumerated enumAnn = getAnnotation( reader.getAnnotationsMethod(), Enumerated.class ); private boolean isNationalized(ParameterType reader) {
if ( enumAnn != null ) { return typeConfiguration.getCurrentBaseSqlTypeIndicators().isNationalized()
return enumAnn.value(); || getAnnotation( reader.getAnnotationsMethod(), Nationalized.class ) != null;
}
return null;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -236,128 +198,10 @@ public class EnumType<T extends Enum<T>>
return null; return null;
} }
private EnumValueConverter<T,?> interpretParameters(Properties parameters) {
JavaType<T> javaType = typeConfiguration.getJavaTypeRegistry().getDescriptor( enumClass );
final EnumJavaType<T> enumJavaType = (EnumJavaType<T>) javaType;
// this method should only be called for hbm.xml handling
assert parameters.get( PARAMETER_TYPE ) == null;
final LocalJdbcTypeIndicators localIndicators = new LocalJdbcTypeIndicators(
// use ORDINAL as default for hbm.xml mappings
jakarta.persistence.EnumType.ORDINAL,
// Is there a reasonable value here? Limits the
// number of enums that can be stored:
// 1 = 10
// 2 = 100
// etc
-1L,
null
);
if ( parameters.containsKey( NAMED ) ) {
final boolean useNamed = ConfigurationHelper.getBoolean( NAMED, parameters );
return getConverter( enumJavaType, localIndicators, useNamed );
}
if ( parameters.containsKey( TYPE ) ) {
final int type = Integer.decode( (String) parameters.get( TYPE ) );
return getConverterForType( enumJavaType, localIndicators, type );
}
final JavaType<? extends Number> relationalJavaType = resolveRelationalJavaType( localIndicators, enumJavaType );
// the fallback
return new OrdinalEnumValueConverter<>(
enumJavaType,
relationalJavaType.getRecommendedJdbcType( localIndicators ),
relationalJavaType
);
}
private JavaType<String> getStringType() {
return typeConfiguration.getJavaTypeRegistry().getDescriptor(String.class);
}
private EnumValueConverter<T,?> getConverter(
EnumJavaType<T> enumJavaType,
EnumType<T>.LocalJdbcTypeIndicators localIndicators,
boolean useNamed) {
if (useNamed) {
return new NamedEnumValueConverter<>(
enumJavaType,
getStringType().getRecommendedJdbcType( localIndicators ),
getStringType()
);
}
else {
final JavaType<? extends Number> relationalJavaType = resolveRelationalJavaType( localIndicators, enumJavaType );
return new OrdinalEnumValueConverter<>(
enumJavaType,
relationalJavaType.getRecommendedJdbcType( localIndicators ),
relationalJavaType
);
}
}
private EnumValueConverter<T,?> getConverterForType(
EnumJavaType<T> enumJavaType,
LocalJdbcTypeIndicators localIndicators,
int type) {
if ( isNumericType(type) ) {
final JavaType<? extends Number> relationalJavaType = resolveRelationalJavaType( localIndicators, enumJavaType );
return new OrdinalEnumValueConverter<>(
enumJavaType,
relationalJavaType.getRecommendedJdbcType( localIndicators ),
relationalJavaType
);
}
else if ( isCharacterType(type) ) {
return new NamedEnumValueConverter<>(
enumJavaType,
getStringType().getRecommendedJdbcType( localIndicators ),
getStringType()
);
}
else {
throw new HibernateException(
String.format( "Passed JDBC type code [%s] not recognized as numeric nor character", type )
);
}
}
private boolean isCharacterType(int jdbcTypeCode) {
switch ( jdbcTypeCode ) {
case Types.CHAR:
case Types.LONGVARCHAR:
case Types.VARCHAR: {
return true;
}
default: {
return false;
}
}
}
private boolean isNumericType(int jdbcTypeCode) {
switch ( jdbcTypeCode ) {
case Types.INTEGER:
case Types.NUMERIC:
case Types.SMALLINT:
case Types.TINYINT:
case Types.BIGINT:
case Types.DECIMAL:
case Types.DOUBLE:
case Types.FLOAT: {
return true;
}
default:
return false;
}
}
@Override @Override
public int getSqlType() { public int getSqlType() {
verifyConfigured(); verifyConfigured();
return enumValueConverter.getJdbcTypeCode(); return jdbcType.getJdbcTypeCode();
} }
@Override @Override
@ -378,19 +222,19 @@ public class EnumType<T extends Enum<T>>
@Override @Override
public T nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException { public T nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException {
verifyConfigured(); verifyConfigured();
return enumValueConverter.toDomainValue( jdbcValueExtractor.extract( rs, position, session ) ); return jdbcType.getExtractor( enumJavaType ).extract( rs, position, session );
} }
private void verifyConfigured() { private void verifyConfigured() {
if ( enumValueConverter == null || jdbcValueBinder == null || jdbcValueExtractor == null ) { if ( enumJavaType == null ) {
throw new AssertionFailure( "EnumType (" + enumClass.getName() + ") not properly, fully configured" ); throw new AssertionFailure("EnumType (" + enumClass.getName() + ") not properly, fully configured");
} }
} }
@Override @Override
public void nullSafeSet(PreparedStatement st, T value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException { public void nullSafeSet(PreparedStatement st, T value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
verifyConfigured(); verifyConfigured();
jdbcValueBinder.bind( st, enumValueConverter.toRelationalValue( value ), index, session ); jdbcType.getBinder( enumJavaType ).bind( st, value, index, session );
} }
@Override @Override
@ -405,12 +249,12 @@ public class EnumType<T extends Enum<T>>
@Override @Override
public Serializable disassemble(T value) throws HibernateException { public Serializable disassemble(T value) throws HibernateException {
return (Serializable) enumValueConverter.toRelationalValue( value ); return value;
} }
@Override @Override
public T assemble(Serializable cached, Object owner) throws HibernateException { public T assemble(Serializable cached, Object owner) throws HibernateException {
return enumValueConverter.toDomainValue( cached ); return (T) cached;
} }
@Override @Override
@ -431,45 +275,43 @@ public class EnumType<T extends Enum<T>>
@Override @Override
public String toSqlLiteral(T value) { public String toSqlLiteral(T value) {
verifyConfigured(); verifyConfigured();
return enumValueConverter.toSqlLiteral( value ); return isOrdinal()
? Integer.toString( value.ordinal() )
: "'" + value.name() + "'";
} }
@Override @Override
public String toString(T value) { public String toString(T value) {
verifyConfigured(); verifyConfigured();
return enumValueConverter.getRelationalJavaType().toString( enumValueConverter.toRelationalValue( value ) ); return enumJavaType.toName( value );
} }
@Override @Override
public T fromStringValue(CharSequence sequence) { public T fromStringValue(CharSequence sequence) {
verifyConfigured(); verifyConfigured();
return enumValueConverter.toDomainValue( enumValueConverter.getRelationalJavaType().fromString( sequence ) ); return enumJavaType.fromName( sequence.toString() );
} }
@Override @SuppressWarnings("unchecked") @Override @SuppressWarnings("unchecked")
public String toLoggableString(Object value, SessionFactoryImplementor factory) { public String toLoggableString(Object value, SessionFactoryImplementor factory) {
verifyConfigured(); verifyConfigured();
return enumValueConverter.getDomainJavaType().toString( (T) value ); return enumJavaType.toString( (T) value );
} }
public boolean isOrdinal() { public boolean isOrdinal() {
verifyConfigured(); verifyConfigured();
return enumValueConverter instanceof OrdinalEnumValueConverter; return jdbcType.isInteger();
} }
private class LocalJdbcTypeIndicators implements JdbcTypeIndicators { private class LocalJdbcTypeIndicators implements JdbcTypeIndicators {
private final jakarta.persistence.EnumType enumType; private final jakarta.persistence.EnumType enumType;
private final boolean nationalized;
private final Long columnLength; private final Long columnLength;
private final ParameterType reader;
private LocalJdbcTypeIndicators(jakarta.persistence.EnumType enumType, ParameterType reader) { private LocalJdbcTypeIndicators(jakarta.persistence.EnumType enumType, boolean nationalized, Long columnLength) {
this( enumType, reader.getColumnLengths()[0], reader );
}
private LocalJdbcTypeIndicators(jakarta.persistence.EnumType enumType, Long columnLength, ParameterType reader) {
this.enumType = enumType; this.enumType = enumType;
this.nationalized = nationalized;
this.columnLength = columnLength; this.columnLength = columnLength;
this.reader = reader;
} }
@Override @Override
@ -479,32 +321,14 @@ public class EnumType<T extends Enum<T>>
@Override @Override
public jakarta.persistence.EnumType getEnumeratedType() { public jakarta.persistence.EnumType getEnumeratedType() {
if ( enumType != null ) { return enumType != null ? enumType : typeConfiguration.getCurrentBaseSqlTypeIndicators().getEnumeratedType();
return enumType;
}
return typeConfiguration.getCurrentBaseSqlTypeIndicators().getEnumeratedType();
} }
@Override @Override
public boolean isNationalized() { public boolean isNationalized() {
return isNationalized( reader ); return nationalized;
} }
private boolean isNationalized(ParameterType reader) {
if ( typeConfiguration.getCurrentBaseSqlTypeIndicators().isNationalized() ) {
return true;
}
if ( reader != null ) {
for ( Annotation annotation : reader.getAnnotationsMethod() ) {
if ( annotation instanceof Nationalized ) {
return true;
}
}
}
return false;
}
@Override @Override
public long getColumnLength() { public long getColumnLength() {

View File

@ -18,7 +18,6 @@ import org.hibernate.type.descriptor.JdbcExtractingLogging;
import org.hibernate.type.descriptor.ValueBinder; import org.hibernate.type.descriptor.ValueBinder;
import org.hibernate.type.descriptor.ValueExtractor; import org.hibernate.type.descriptor.ValueExtractor;
import org.hibernate.type.descriptor.WrapperOptions; import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.BasicJavaType;
import org.hibernate.type.descriptor.java.JavaType; import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.jdbc.JdbcLiteralFormatter; import org.hibernate.type.descriptor.jdbc.JdbcLiteralFormatter;
import org.hibernate.type.descriptor.jdbc.JdbcType; import org.hibernate.type.descriptor.jdbc.JdbcType;