HHH-16479 Generic enum in @MappedSuperclass fails with java.lang.IllegalArgumentException: Named type [...] did not implement BasicType nor UserType

This commit is contained in:
Andrea Boriero 2023-04-17 12:43:30 +02:00 committed by Andrea Boriero
parent 962d483684
commit 5a49d04255
1 changed files with 66 additions and 48 deletions

View File

@ -29,6 +29,7 @@ import org.hibernate.resource.beans.spi.ManagedBean;
import org.hibernate.resource.beans.spi.ManagedBeanRegistry; import org.hibernate.resource.beans.spi.ManagedBeanRegistry;
import org.hibernate.type.BasicType; import org.hibernate.type.BasicType;
import org.hibernate.type.CustomType; import org.hibernate.type.CustomType;
import org.hibernate.type.JavaObjectType;
import org.hibernate.type.SerializableType; import org.hibernate.type.SerializableType;
import org.hibernate.type.Type; import org.hibernate.type.Type;
import org.hibernate.type.descriptor.converter.spi.BasicValueConverter; import org.hibernate.type.descriptor.converter.spi.BasicValueConverter;
@ -221,6 +222,27 @@ public class TypeDefinition implements Serializable {
// Series of backward compatible special cases // Series of backward compatible special cases
if ( Serializable.class.isAssignableFrom( typeImplementorClass ) ) { if ( Serializable.class.isAssignableFrom( typeImplementorClass ) ) {
@SuppressWarnings({"rawtypes", "unchecked"})
final SerializableType legacyType = new SerializableType( typeImplementorClass );
return createBasicTypeResolution( legacyType, typeImplementorClass, indicators, typeConfiguration );
}
if ( indicators.getEnumeratedType() != null ) {
assert typeImplementorClass.isInterface();
return createBasicTypeResolution( new JavaObjectType(), typeImplementorClass, indicators, typeConfiguration );
}
throw new IllegalArgumentException(
"Named type [" + typeImplementorClass + "] did not implement BasicType nor UserType"
);
}
private static BasicValue.Resolution<Object> createBasicTypeResolution(
BasicType<?> type,
Class<?> typeImplementorClass,
JdbcTypeIndicators indicators,
TypeConfiguration typeConfiguration
) {
final JavaType<Serializable> jtd = typeConfiguration final JavaType<Serializable> jtd = typeConfiguration
.getJavaTypeRegistry() .getJavaTypeRegistry()
.resolveDescriptor( typeImplementorClass ); .resolveDescriptor( typeImplementorClass );
@ -230,8 +252,6 @@ public class TypeDefinition implements Serializable {
typeConfiguration.getBasicTypeRegistry().resolve( jtd, jdbcType ), typeConfiguration.getBasicTypeRegistry().resolve( jtd, jdbcType ),
jtd jtd
); );
@SuppressWarnings({"rawtypes", "unchecked"})
final SerializableType legacyType = new SerializableType( typeImplementorClass );
return new BasicValue.Resolution<>() { return new BasicValue.Resolution<>() {
@Override @Override
@ -239,12 +259,14 @@ public class TypeDefinition implements Serializable {
return resolved; return resolved;
} }
@Override @SuppressWarnings({"rawtypes", "unchecked"}) @Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public BasicType getLegacyResolvedBasicType() { public BasicType getLegacyResolvedBasicType() {
return legacyType; return type;
} }
@Override @SuppressWarnings({"rawtypes", "unchecked"}) @Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public JavaType getDomainJavaType() { public JavaType getDomainJavaType() {
return resolved.getMappedJavaType(); return resolved.getMappedJavaType();
} }
@ -264,7 +286,8 @@ public class TypeDefinition implements Serializable {
return resolved.getValueConverter(); return resolved.getValueConverter();
} }
@Override @SuppressWarnings({"rawtypes", "unchecked"}) @Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public MutabilityPlan getMutabilityPlan() { public MutabilityPlan getMutabilityPlan() {
// a TypeDefinition does not explicitly provide a MutabilityPlan (yet?) // a TypeDefinition does not explicitly provide a MutabilityPlan (yet?)
return resolved.isMutable() return resolved.isMutable()
@ -274,11 +297,6 @@ public class TypeDefinition implements Serializable {
}; };
} }
throw new IllegalArgumentException(
"Named type [" + typeImplementorClass + "] did not implement BasicType nor UserType"
);
}
private static Object instantiateType( private static Object instantiateType(
StandardServiceRegistry serviceRegistry, StandardServiceRegistry serviceRegistry,
MetadataBuildingOptions buildingOptions, MetadataBuildingOptions buildingOptions,