HHH-16125 final cleanup of legacy enum converters stuff

This commit is contained in:
Gavin 2023-04-29 13:54:07 +02:00 committed by Gavin King
parent 15d20113e7
commit 20b157000c
7 changed files with 23 additions and 211 deletions

View File

@ -23,7 +23,6 @@ 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.type.descriptor.converter.spi.BasicValueConverter;
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;
@ -68,14 +67,13 @@ public class EnumType<T extends Enum<T>>
public EnumType() { public EnumType() {
} }
@Override public Class<T> getEnumClass() {
public JdbcType getJdbcType(TypeConfiguration typeConfiguration) { return enumClass;
return jdbcType;
} }
@Override @Override
public BasicValueConverter<T, Object> getValueConverter() { public JdbcType getJdbcType(TypeConfiguration typeConfiguration) {
return null; return jdbcType;
} }
/** /**
@ -126,9 +124,6 @@ public class EnumType<T extends Enum<T>>
else if ( reader != null ) { else if ( reader != null ) {
enumClass = (Class<T>) reader.getReturnedClass().asSubclass( Enum.class ); enumClass = (Class<T>) reader.getReturnedClass().asSubclass( Enum.class );
} }
else {
throw new AssertionFailure( "No enum class" );
}
final JavaType<T> descriptor = typeConfiguration.getJavaTypeRegistry().getDescriptor( enumClass ); final JavaType<T> descriptor = typeConfiguration.getJavaTypeRegistry().getDescriptor( enumClass );
enumJavaType = (EnumJavaType<T>) descriptor; enumJavaType = (EnumJavaType<T>) descriptor;
@ -185,7 +180,7 @@ public class EnumType<T extends Enum<T>>
private boolean isNationalized(ParameterType reader) { private boolean isNationalized(ParameterType reader) {
return typeConfiguration.getCurrentBaseSqlTypeIndicators().isNationalized() return typeConfiguration.getCurrentBaseSqlTypeIndicators().isNationalized()
|| getAnnotation( reader.getAnnotationsMethod(), Nationalized.class ) != null; || reader!=null && getAnnotation( reader.getAnnotationsMethod(), Nationalized.class ) != null;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

View File

@ -1,68 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.type.descriptor.converter.internal;
import java.io.Serializable;
import org.hibernate.type.descriptor.converter.spi.EnumValueConverter;
import org.hibernate.type.descriptor.java.EnumJavaType;
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.jdbc.JdbcType;
/**
* BasicValueConverter handling the conversion of an enum based on
* JPA {@link jakarta.persistence.EnumType#STRING} strategy (storing the name)
*
* @author Steve Ebersole
*
* @deprecated we no longer use converters to handle enum mappings
*/
@Deprecated(since="6.3", forRemoval=true)
public class NamedEnumValueConverter<E extends Enum<E>> implements EnumValueConverter<E,String>, Serializable {
private final EnumJavaType<E> domainTypeDescriptor;
private final JdbcType jdbcType;
private final JavaType<String> relationalTypeDescriptor;
public NamedEnumValueConverter(
EnumJavaType<E> domainTypeDescriptor,
JdbcType jdbcType,
JavaType<String> relationalTypeDescriptor) {
this.domainTypeDescriptor = domainTypeDescriptor;
this.jdbcType = jdbcType;
this.relationalTypeDescriptor = relationalTypeDescriptor;
}
@Override
public EnumJavaType<E> getDomainJavaType() {
return domainTypeDescriptor;
}
@Override
public JavaType<String> getRelationalJavaType() {
return relationalTypeDescriptor;
}
@Override
public E toDomainValue(String relationalForm) {
return domainTypeDescriptor.fromName( relationalForm );
}
@Override
public String toRelationalValue(E domainForm) {
return domainTypeDescriptor.toName( domainForm );
}
@Override
public int getJdbcTypeCode() {
return jdbcType.getDefaultSqlTypeCode();
}
@Override
public String toSqlLiteral(Object value) {
return "'" + ( (Enum<?>) value ).name() + "'";
}
}

View File

@ -1,69 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.type.descriptor.converter.internal;
import java.io.Serializable;
import org.hibernate.type.descriptor.converter.spi.EnumValueConverter;
import org.hibernate.type.descriptor.java.EnumJavaType;
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.jdbc.JdbcType;
/**
* BasicValueConverter handling the conversion of an enum based on
* JPA {@link jakarta.persistence.EnumType#ORDINAL} strategy (storing the ordinal)
*
* @author Steve Ebersole
*
* @deprecated we no longer use converters to handle enum mappings
*/
@Deprecated(since="6.3", forRemoval=true)
public class OrdinalEnumValueConverter<E extends Enum<E>, N extends Number> implements EnumValueConverter<E, N>, Serializable {
private final EnumJavaType<E> enumJavaType;
private final JdbcType jdbcType;
private final JavaType<N> relationalJavaType;
public OrdinalEnumValueConverter(
EnumJavaType<E> enumJavaType,
JdbcType jdbcType,
JavaType<N> relationalJavaType) {
this.enumJavaType = enumJavaType;
this.jdbcType = jdbcType;
this.relationalJavaType = relationalJavaType;
}
@Override
public E toDomainValue(Number relationalForm) {
return enumJavaType.fromOrdinal( relationalForm == null ? null : relationalForm.intValue() );
}
@Override
public N toRelationalValue(E domainForm) {
return relationalJavaType.wrap( enumJavaType.toOrdinal( domainForm ), null );
}
@Override
public int getJdbcTypeCode() {
return jdbcType.getDefaultSqlTypeCode();
}
@Override
public EnumJavaType<E> getDomainJavaType() {
return enumJavaType;
}
@Override
public JavaType<N> getRelationalJavaType() {
return relationalJavaType;
}
@Override
public String toSqlLiteral(Object value) {
return Integer.toString( ( (Enum<?>) value ).ordinal() );
}
}

View File

@ -11,14 +11,9 @@
* Hibernate {@link org.hibernate.type.Type} system. The main contract is * Hibernate {@link org.hibernate.type.Type} system. The main contract is
* {@link org.hibernate.type.descriptor.converter.spi.BasicValueConverter}. * {@link org.hibernate.type.descriptor.converter.spi.BasicValueConverter}.
* <p> * <p>
* All basic value conversions are defined by this namespace, including: * All basic value conversions are defined by this namespace, including
* <ul> * support for {@link jakarta.persistence.AttributeConverter} via
* <li>Java {@code enum} conversions via * {@link org.hibernate.type.descriptor.converter.spi.JpaAttributeConverter}.
* {@link org.hibernate.type.descriptor.converter.spi.EnumValueConverter},
* and
* <li>support for {@link jakarta.persistence.AttributeConverter} via
* {@link org.hibernate.type.descriptor.converter.spi.JpaAttributeConverter}.
* </ul>
* *
* @see org.hibernate.type.descriptor.converter.spi.BasicValueConverter * @see org.hibernate.type.descriptor.converter.spi.BasicValueConverter
*/ */

View File

@ -1,26 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.type.descriptor.converter.spi;
import org.hibernate.Incubating;
import org.hibernate.type.descriptor.java.EnumJavaType;
/**
* {@link BasicValueConverter} extension for enum-specific support
*
* @author Steve Ebersole
*/
@Incubating
public interface EnumValueConverter<O extends Enum<O>, R> extends BasicValueConverter<O,R> {
@Override
EnumJavaType<O> getDomainJavaType();
int getJdbcTypeCode();
String toSqlLiteral(Object value);
}

View File

@ -12,13 +12,9 @@
* <p> * <p>
* The main contract is * The main contract is
* {@link org.hibernate.type.descriptor.converter.spi.BasicValueConverter}, * {@link org.hibernate.type.descriptor.converter.spi.BasicValueConverter},
* with specializations: * which is specialized by
* <ul> * {@link org.hibernate.type.descriptor.converter.spi.JpaAttributeConverter}
* <li>{@link org.hibernate.type.descriptor.converter.spi.EnumValueConverter} * for adapting for JPA {@link jakarta.persistence.AttributeConverter}s.
* for Java {@code enum} conversions, and
* <li>{@link org.hibernate.type.descriptor.converter.spi.JpaAttributeConverter}
* for adapting for JPA {@link jakarta.persistence.AttributeConverter}.
* </ul>
* *
* @see org.hibernate.type.descriptor.converter.spi.BasicValueConverter * @see org.hibernate.type.descriptor.converter.spi.BasicValueConverter
*/ */

View File

@ -16,11 +16,9 @@ import org.hibernate.envers.internal.entities.mapper.SimpleMapperBuilder;
import org.hibernate.mapping.SimpleValue; import org.hibernate.mapping.SimpleValue;
import org.hibernate.mapping.Value; import org.hibernate.mapping.Value;
import org.hibernate.type.BasicType; import org.hibernate.type.BasicType;
import org.hibernate.type.ConvertedBasicType; import org.hibernate.type.CustomType;
import org.hibernate.type.EnumType; import org.hibernate.type.EnumType;
import org.hibernate.type.Type; import org.hibernate.type.Type;
import org.hibernate.type.descriptor.converter.internal.NamedEnumValueConverter;
import org.hibernate.type.descriptor.converter.spi.EnumValueConverter;
/** /**
* Generates metadata for basic properties: immutable types (including enums). * Generates metadata for basic properties: immutable types (including enums).
@ -42,7 +40,7 @@ public final class BasicMetadataGenerator {
boolean key) { boolean key) {
if ( value.getType() instanceof BasicType ) { if ( value.getType() instanceof BasicType ) {
if ( attributeContainer != null ) { if ( attributeContainer != null ) {
BasicAttribute attribute = buildProperty(propertyAuditingData, value, insertable, key ); BasicAttribute attribute = buildProperty( propertyAuditingData, value, insertable, key );
attributeContainer.addAttribute( attribute ); attributeContainer.addAttribute( attribute );
if ( isAddNestedType( value ) ) { if ( isAddNestedType( value ) ) {
@ -62,21 +60,18 @@ public final class BasicMetadataGenerator {
return false; return false;
} }
private void mapEnumerationType(TypeSpecification typeDefinition, Type type, Properties parameters) { private void mapEnumerationType(TypeSpecification typeDefinition, EnumType type, Properties parameters) {
if ( parameters.getProperty( EnumType.ENUM ) != null ) { if ( parameters.getProperty( EnumType.ENUM ) != null ) {
typeDefinition.setParameter( EnumType.ENUM, parameters.getProperty( EnumType.ENUM ) ); typeDefinition.setParameter( EnumType.ENUM, parameters.getProperty( EnumType.ENUM ) );
} }
else { else {
typeDefinition.setParameter( EnumType.ENUM, type.getReturnedClass().getName() ); typeDefinition.setParameter( EnumType.ENUM, type.getEnumClass().getName() );
} }
if ( parameters.getProperty( EnumType.NAMED ) != null ) { if ( parameters.getProperty( EnumType.NAMED ) != null ) {
typeDefinition.setParameter( EnumType.NAMED, parameters.getProperty( EnumType.NAMED ) ); typeDefinition.setParameter( EnumType.NAMED, parameters.getProperty( EnumType.NAMED ) );
} }
else { else {
final ConvertedBasicType<?> convertedType = (ConvertedBasicType<?>) type; typeDefinition.setParameter( EnumType.NAMED, Boolean.toString( !type.isOrdinal() ) );
final EnumValueConverter<?, ?> valueConverter = (EnumValueConverter<?, ?>) convertedType.getValueConverter();
final boolean isNamed = valueConverter instanceof NamedEnumValueConverter;
typeDefinition.setParameter( EnumType.NAMED, Boolean.toString( isNamed ) );
} }
} }
@ -103,12 +98,13 @@ public final class BasicMetadataGenerator {
final Properties typeParameters = value.getTypeParameters(); final Properties typeParameters = value.getTypeParameters();
final String typeName = getBasicTypeName( value.getType() ); final String typeName = getBasicTypeName( value.getType() );
final TypeSpecification type = new TypeSpecification(typeName ); final TypeSpecification typeSpecification = new TypeSpecification( typeName );
attribute.setType( type ); attribute.setType( typeSpecification );
if ( isEnumType( value.getType(), typeName ) ) { Type type = value.getType();
// Proper handling of enumeration type if ( type instanceof CustomType && ((CustomType<?>) type).getUserType() instanceof EnumType ) {
mapEnumerationType( type, value.getType(), typeParameters ); // Proper handling of nasty legacy EnumType
mapEnumerationType( typeSpecification, (EnumType) ((CustomType<?>) type).getUserType(), typeParameters );
} }
else { else {
// By default, copying all Hibernate properties // By default, copying all Hibernate properties
@ -116,7 +112,7 @@ public final class BasicMetadataGenerator {
final String keyType = (String) object; final String keyType = (String) object;
final String property = typeParameters.getProperty( keyType ); final String property = typeParameters.getProperty( keyType );
if ( property != null ) { if ( property != null ) {
type.setParameter( keyType, property ); typeSpecification.setParameter( keyType, property );
} }
} }
} }
@ -130,11 +126,4 @@ public final class BasicMetadataGenerator {
return typeName; return typeName;
} }
private boolean isEnumType(Type type, String typeName) {
if ( type instanceof ConvertedBasicType ) {
final ConvertedBasicType<?> convertedType = (ConvertedBasicType<?>) type;
return convertedType.getValueConverter() instanceof EnumValueConverter;
}
return false;
}
} }