fix generic typing and remove warning suppression

This commit is contained in:
Gavin King 2024-11-16 10:18:27 +01:00
parent 80a1b36477
commit e1f62098e2
3 changed files with 30 additions and 32 deletions

View File

@ -187,15 +187,12 @@ public class BooleanJavaType extends AbstractClassJavaType<Boolean> implements
}
@Override
public String getCheckCondition(String columnName, JdbcType jdbcType, BasicValueConverter<?, ?> converter, Dialect dialect) {
public String getCheckCondition(String columnName, JdbcType jdbcType, BasicValueConverter<Boolean, ?> converter, Dialect dialect) {
if ( converter != null ) {
if ( jdbcType.isString() ) {
@SuppressWarnings("unchecked")
final BasicValueConverter<Boolean, ?> stringConverter =
(BasicValueConverter<Boolean, ?>) converter;
final Object falseValue = stringConverter.toRelationalValue( false );
final Object trueValue = stringConverter.toRelationalValue( true );
final String[] values = getPossibleStringValues( stringConverter, falseValue, trueValue );
final Object falseValue = converter.toRelationalValue( false );
final Object trueValue = converter.toRelationalValue( true );
final String[] values = getPossibleStringValues( converter, falseValue, trueValue );
return dialect.getCheckCondition( columnName, values );
}
else if ( jdbcType.isInteger() ) {

View File

@ -8,7 +8,6 @@ import java.util.Set;
import org.hibernate.boot.model.process.internal.EnumeratedValueConverter;
import org.hibernate.dialect.Dialect;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.type.SqlTypes;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.converter.spi.BasicValueConverter;
@ -19,6 +18,7 @@ import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry;
import jakarta.persistence.EnumType;
import static jakarta.persistence.EnumType.ORDINAL;
import static org.hibernate.internal.util.collections.CollectionHelper.setOfSize;
import static org.hibernate.type.SqlTypes.CHAR;
import static org.hibernate.type.SqlTypes.ENUM;
import static org.hibernate.type.SqlTypes.NAMED_ENUM;
@ -36,21 +36,21 @@ import static org.hibernate.type.SqlTypes.VARCHAR;
* @author Steve Ebersole
*/
public class EnumJavaType<T extends Enum<T>> extends AbstractClassJavaType<T> {
public EnumJavaType(Class<T> type) {
super( type, ImmutableMutabilityPlan.instance() );
}
@Override
public JdbcType getRecommendedJdbcType(JdbcTypeIndicators context) {
final JdbcTypeRegistry jdbcTypeRegistry = context.getTypeConfiguration().getJdbcTypeRegistry();
final EnumType type = context.getEnumeratedType();
final int sqlType = getSqlType( context, type, jdbcTypeRegistry );
return jdbcTypeRegistry.getDescriptor( sqlType );
return context.getTypeConfiguration().getJdbcTypeRegistry().getDescriptor( sqlType( context ) );
}
private int getSqlType(JdbcTypeIndicators context, EnumType type, JdbcTypeRegistry jdbcTypeRegistry) {
private int sqlType(JdbcTypeIndicators context) {
final EnumType enumType = context.getEnumeratedType();
final boolean preferNativeEnumTypes = context.isPreferNativeEnumTypesEnabled();
return switch ( type == null ? ORDINAL : type ) {
final JdbcTypeRegistry jdbcTypeRegistry = context.getTypeConfiguration().getJdbcTypeRegistry();
return switch ( enumType == null ? ORDINAL : enumType ) {
case ORDINAL:
if ( preferNativeEnumTypes && jdbcTypeRegistry.hasRegisteredDescriptor( ORDINAL_ENUM ) ) {
yield ORDINAL_ENUM;
@ -142,8 +142,9 @@ public class EnumJavaType<T extends Enum<T>> extends AbstractClassJavaType<T> {
else if ( value instanceof Number number ) {
return fromLong( number.longValue() );
}
else {
return (T) value;
}
}
/**
@ -255,7 +256,7 @@ public class EnumJavaType<T extends Enum<T>> extends AbstractClassJavaType<T> {
}
@Override
public String getCheckCondition(String columnName, JdbcType jdbcType, BasicValueConverter<?, ?> converter, Dialect dialect) {
public String getCheckCondition(String columnName, JdbcType jdbcType, BasicValueConverter<T, ?> converter, Dialect dialect) {
if ( converter != null
&& jdbcType.getDefaultSqlTypeCode() != NAMED_ENUM ) {
return renderConvertedEnumCheckConstraint( columnName, jdbcType, converter, dialect );
@ -272,16 +273,19 @@ public class EnumJavaType<T extends Enum<T>> extends AbstractClassJavaType<T> {
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private String renderConvertedEnumCheckConstraint(
String columnName,
JdbcType jdbcType,
BasicValueConverter<?, ?> converter,
BasicValueConverter<T, ?> converter,
Dialect dialect) {
final Set valueSet;
final Set<?> valueSet = valueSet( jdbcType, converter );
return valueSet == null ? null : dialect.getCheckCondition( columnName, valueSet, jdbcType );
}
private <R> Set<R> valueSet(JdbcType jdbcType, BasicValueConverter<T,R> converter) {
// for `@EnumeratedValue` we already have the possible values...
if ( converter instanceof EnumeratedValueConverter enumeratedValueConverter ) {
valueSet = enumeratedValueConverter.getRelationalValueSet();
if ( converter instanceof EnumeratedValueConverter<T,R> enumeratedValueConverter ) {
return enumeratedValueConverter.getRelationalValueSet();
}
else {
if ( !SqlTypes.isIntegral( jdbcType.getJdbcTypeCode() )
@ -290,17 +294,14 @@ public class EnumJavaType<T extends Enum<T>> extends AbstractClassJavaType<T> {
// INTEGER, SMALLINT, TINYINT, (N)CHAR, (N)VARCHAR, LONG(N)VARCHAR
return null;
}
final Class<T> javaTypeClass = getJavaTypeClass();
final T[] enumConstants = javaTypeClass.getEnumConstants();
valueSet = CollectionHelper.setOfSize( enumConstants.length );
for ( T enumConstant : enumConstants ) {
//noinspection unchecked
final Object relationalValue = ( (BasicValueConverter) converter ).toRelationalValue( enumConstant );
valueSet.add( relationalValue );
else {
final T[] enumConstants = getJavaTypeClass().getEnumConstants();
final Set<R> valueSet = setOfSize( enumConstants.length );
for ( T enumConstant : enumConstants ) {
valueSet.add( converter.toRelationalValue( enumConstant ) );
}
return valueSet;
}
}
return dialect.getCheckCondition( columnName, valueSet, jdbcType );
}
}

View File

@ -349,7 +349,7 @@ public interface JavaType<T> extends Serializable {
* @since 6.2
*/
@Incubating
default String getCheckCondition(String columnName, JdbcType jdbcType, BasicValueConverter<?, ?> converter, Dialect dialect) {
default String getCheckCondition(String columnName, JdbcType jdbcType, BasicValueConverter<T, ?> converter, Dialect dialect) {
return null;
}
}