HHH-17739 fix NPE for field with unsupported collection type
cleanups and warning fixes in TypeConfiguration and BasicValue
This commit is contained in:
parent
df8981f53f
commit
1e5eaaf50d
|
@ -40,7 +40,6 @@ import org.hibernate.dialect.Dialect;
|
|||
import org.hibernate.engine.jdbc.Size;
|
||||
import org.hibernate.internal.CoreLogging;
|
||||
import org.hibernate.internal.CoreMessageLogger;
|
||||
import org.hibernate.internal.util.ReflectHelper;
|
||||
import org.hibernate.internal.util.StringHelper;
|
||||
import org.hibernate.internal.util.collections.CollectionHelper;
|
||||
import org.hibernate.metamodel.mapping.JdbcMapping;
|
||||
|
@ -82,6 +81,7 @@ import jakarta.persistence.EnumType;
|
|||
import jakarta.persistence.TemporalType;
|
||||
|
||||
import static java.lang.Boolean.parseBoolean;
|
||||
import static org.hibernate.internal.util.ReflectHelper.reflectedPropertyType;
|
||||
import static org.hibernate.internal.util.collections.CollectionHelper.isNotEmpty;
|
||||
import static org.hibernate.mapping.MappingHelper.injectParameters;
|
||||
|
||||
|
@ -103,6 +103,7 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
|
|||
private Function<TypeConfiguration, java.lang.reflect.Type> implicitJavaTypeAccess;
|
||||
|
||||
private EnumType enumerationStyle;
|
||||
@SuppressWarnings("deprecation")
|
||||
private TemporalType temporalPrecision;
|
||||
private TimeZoneStorageType timeZoneStorageType;
|
||||
private boolean isSoftDelete;
|
||||
|
@ -234,8 +235,7 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
|
|||
public long getColumnLength() {
|
||||
final Selectable selectable = getColumn();
|
||||
if ( selectable instanceof Column ) {
|
||||
final Column column = (Column) selectable;
|
||||
final Long length = column.getLength();
|
||||
final Long length = ( (Column) selectable ).getLength();
|
||||
return length == null ? NO_COLUMN_LENGTH : length;
|
||||
}
|
||||
else {
|
||||
|
@ -248,8 +248,9 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
|
|||
final Selectable selectable = getColumn();
|
||||
if ( selectable instanceof Column ) {
|
||||
final Column column = (Column) selectable;
|
||||
if ( column.getTemporalPrecision() != null ) {
|
||||
return column.getTemporalPrecision();
|
||||
final Integer temporalPrecision = column.getTemporalPrecision();
|
||||
if ( temporalPrecision != null ) {
|
||||
return temporalPrecision;
|
||||
}
|
||||
final Integer precision = column.getPrecision();
|
||||
return precision == null ? NO_COLUMN_PRECISION : precision;
|
||||
|
@ -263,8 +264,7 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
|
|||
public int getColumnScale() {
|
||||
final Selectable selectable = getColumn();
|
||||
if ( selectable instanceof Column ) {
|
||||
final Column column = (Column) selectable;
|
||||
final Integer scale = column.getScale();
|
||||
final Integer scale = ( (Column) selectable ).getScale();
|
||||
return scale == null ? NO_COLUMN_SCALE : scale;
|
||||
}
|
||||
else {
|
||||
|
@ -284,8 +284,9 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
|
|||
super.copyTypeFrom( sourceValue );
|
||||
if ( sourceValue instanceof BasicValue ) {
|
||||
final BasicValue basicValue = (BasicValue) sourceValue;
|
||||
this.resolution = basicValue.resolution;
|
||||
this.implicitJavaTypeAccess = (typeConfiguration) -> basicValue.implicitJavaTypeAccess.apply( typeConfiguration );
|
||||
resolution = basicValue.resolution;
|
||||
implicitJavaTypeAccess =
|
||||
typeConfiguration -> basicValue.implicitJavaTypeAccess.apply( typeConfiguration );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -447,33 +448,55 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
|
|||
getBuildingContext()
|
||||
);
|
||||
}
|
||||
|
||||
if ( isVersion() ) {
|
||||
else if ( isVersion() ) {
|
||||
return VersionResolution.from( implicitJavaTypeAccess, timeZoneStorageType, getBuildingContext() );
|
||||
}
|
||||
|
||||
else {
|
||||
// determine JavaType if we can
|
||||
final BasicJavaType<?> explicitJavaType = explicitJavaTypeAccess == null
|
||||
? null
|
||||
final BasicJavaType<?> explicitJavaType = getExplicitJavaType();
|
||||
final JavaType<?> javaType = determineJavaType( explicitJavaType );
|
||||
final ConverterDescriptor converterDescriptor = getConverterDescriptor( javaType );
|
||||
return converterDescriptor != null
|
||||
? converterResolution( javaType, converterDescriptor )
|
||||
: resolution( explicitJavaType, javaType );
|
||||
}
|
||||
}
|
||||
|
||||
private BasicJavaType<?> getExplicitJavaType() {
|
||||
return explicitJavaTypeAccess == null ? null
|
||||
: explicitJavaTypeAccess.apply( getTypeConfiguration() );
|
||||
}
|
||||
|
||||
JavaType<?> javaType = determineJavaType( explicitJavaType );
|
||||
ConverterDescriptor attributeConverterDescriptor = getAttributeConverterDescriptor();
|
||||
|
||||
private ConverterDescriptor getConverterDescriptor(JavaType<?> javaType) {
|
||||
final ConverterDescriptor converterDescriptor = getAttributeConverterDescriptor();
|
||||
if ( isSoftDelete() ) {
|
||||
assert attributeConverterDescriptor != null;
|
||||
final boolean conversionWasUnspecified = SoftDelete.UnspecifiedConversion.class.equals( attributeConverterDescriptor.getAttributeConverterClass() );
|
||||
assert converterDescriptor != null;
|
||||
final ConverterDescriptor softDeleteConverterDescriptor =
|
||||
getSoftDeleteConverterDescriptor( converterDescriptor, javaType);
|
||||
return getSoftDeleteStrategy() == SoftDeleteType.ACTIVE
|
||||
? new ReversedConverterDescriptor<>( softDeleteConverterDescriptor )
|
||||
: softDeleteConverterDescriptor;
|
||||
}
|
||||
else {
|
||||
return converterDescriptor;
|
||||
}
|
||||
}
|
||||
|
||||
private ConverterDescriptor getSoftDeleteConverterDescriptor(
|
||||
ConverterDescriptor attributeConverterDescriptor, JavaType<?> javaType) {
|
||||
final boolean conversionWasUnspecified =
|
||||
SoftDelete.UnspecifiedConversion.class.equals( attributeConverterDescriptor.getAttributeConverterClass() );
|
||||
if ( conversionWasUnspecified ) {
|
||||
final JdbcType jdbcType = BooleanJdbcType.INSTANCE.resolveIndicatedType( this, javaType);
|
||||
if ( jdbcType.isNumber() ) {
|
||||
attributeConverterDescriptor = new InstanceBasedConverterDescriptor(
|
||||
return new InstanceBasedConverterDescriptor(
|
||||
NumericBooleanConverter.INSTANCE,
|
||||
getBuildingContext().getBootstrapContext().getClassmateContext()
|
||||
);
|
||||
}
|
||||
else if ( jdbcType.isString() ) {
|
||||
// here we pick 'T' / 'F' storage, though 'Y' / 'N' is equally valid - its 50/50
|
||||
attributeConverterDescriptor = new InstanceBasedConverterDescriptor(
|
||||
return new InstanceBasedConverterDescriptor(
|
||||
TrueFalseConverter.INSTANCE,
|
||||
getBuildingContext().getBootstrapContext().getClassmateContext()
|
||||
);
|
||||
|
@ -481,23 +504,17 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
|
|||
else {
|
||||
// should indicate BIT or BOOLEAN == no conversion needed
|
||||
// - we still create the converter to properly set up JDBC type, etc
|
||||
attributeConverterDescriptor = new InstanceBasedConverterDescriptor(
|
||||
return new InstanceBasedConverterDescriptor(
|
||||
PassThruSoftDeleteConverter.INSTANCE,
|
||||
getBuildingContext().getBootstrapContext().getClassmateContext()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ( getSoftDeleteStrategy() == SoftDeleteType.ACTIVE ) {
|
||||
attributeConverterDescriptor = new ReversedConverterDescriptor<>( attributeConverterDescriptor );
|
||||
else {
|
||||
return attributeConverterDescriptor;
|
||||
}
|
||||
}
|
||||
|
||||
return attributeConverterDescriptor != null
|
||||
? converterResolution( javaType, attributeConverterDescriptor )
|
||||
: resolution( explicitJavaType, javaType );
|
||||
}
|
||||
|
||||
private static class ReversedConverterDescriptor<R> implements ConverterDescriptor {
|
||||
private final ConverterDescriptor underlyingDescriptor;
|
||||
|
||||
|
@ -680,11 +697,10 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
|
|||
&& !attributeConverterDescriptor.getDomainValueResolvedType().getErasedType()
|
||||
.isAssignableFrom( javaType.getJavaTypeClass() ) ) {
|
||||
// In this case, the converter applies to the element of a BasicPluralJavaType
|
||||
final BasicPluralJavaType<?> containerJtd = (BasicPluralJavaType<?>) javaType;
|
||||
final BasicType registeredElementType = converterResolution.getLegacyResolvedBasicType();
|
||||
final Selectable column = getColumn();
|
||||
final BasicType<?> registeredType = registeredElementType == null ? null
|
||||
: containerJtd.resolveType(
|
||||
: ( (BasicPluralJavaType<?>) javaType ).resolveType(
|
||||
getTypeConfiguration(),
|
||||
getDialect(),
|
||||
registeredElementType,
|
||||
|
@ -730,66 +746,76 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
|
|||
}
|
||||
|
||||
private JavaType<?> determineReflectedJavaType() {
|
||||
final java.lang.reflect.Type impliedJavaType;
|
||||
|
||||
final TypeConfiguration typeConfiguration = getTypeConfiguration();
|
||||
final java.lang.reflect.Type impliedJavaType = impliedJavaType( typeConfiguration );
|
||||
if ( impliedJavaType == null ) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
resolvedJavaType = impliedJavaType;
|
||||
return javaType( typeConfiguration, impliedJavaType );
|
||||
}
|
||||
}
|
||||
|
||||
private java.lang.reflect.Type impliedJavaType(TypeConfiguration typeConfiguration) {
|
||||
if ( resolvedJavaType != null ) {
|
||||
impliedJavaType = resolvedJavaType;
|
||||
return resolvedJavaType;
|
||||
}
|
||||
else if ( implicitJavaTypeAccess != null ) {
|
||||
impliedJavaType = implicitJavaTypeAccess.apply( typeConfiguration );
|
||||
return implicitJavaTypeAccess.apply(typeConfiguration);
|
||||
}
|
||||
else if ( ownerName != null && propertyName != null ) {
|
||||
impliedJavaType = ReflectHelper.reflectedPropertyType(
|
||||
ownerName,
|
||||
propertyName,
|
||||
getServiceRegistry().requireService( ClassLoaderService.class )
|
||||
);
|
||||
return reflectedPropertyType( ownerName, propertyName,
|
||||
getServiceRegistry().requireService( ClassLoaderService.class ) );
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
|
||||
resolvedJavaType = impliedJavaType;
|
||||
|
||||
if ( impliedJavaType == null ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private JavaType<Object> javaType(TypeConfiguration typeConfiguration, java.lang.reflect.Type impliedJavaType) {
|
||||
final JavaType<Object> javaType = typeConfiguration.getJavaTypeRegistry().findDescriptor( impliedJavaType );
|
||||
return javaType == null ? specialJavaType( typeConfiguration, impliedJavaType ) : javaType;
|
||||
}
|
||||
|
||||
private JavaType<Object> specialJavaType(
|
||||
TypeConfiguration typeConfiguration,
|
||||
java.lang.reflect.Type impliedJavaType) {
|
||||
final JavaTypeRegistry javaTypeRegistry = typeConfiguration.getJavaTypeRegistry();
|
||||
final JavaType<Object> javaType = javaTypeRegistry.findDescriptor( impliedJavaType );
|
||||
final MutabilityPlan<Object> explicitMutabilityPlan = explicitMutabilityPlanAccess != null
|
||||
? explicitMutabilityPlanAccess.apply( typeConfiguration )
|
||||
: null;
|
||||
final MutabilityPlan<Object> determinedMutabilityPlan = explicitMutabilityPlan != null
|
||||
? explicitMutabilityPlan
|
||||
: RegistryHelper.INSTANCE.determineMutabilityPlan( impliedJavaType, typeConfiguration );
|
||||
if ( javaType == null ) {
|
||||
if ( jdbcTypeCode != null ) {
|
||||
// Construct special JavaType instances for JSON/XML types which can report recommended JDBC types
|
||||
// and implement toString/fromString as well as copying based on FormatMapper operations
|
||||
switch ( jdbcTypeCode ) {
|
||||
case SqlTypes.JSON:
|
||||
final JavaType<Object> jsonJavaType = new JsonJavaType<>(
|
||||
impliedJavaType,
|
||||
determinedMutabilityPlan,
|
||||
typeConfiguration
|
||||
);
|
||||
final JavaType<Object> jsonJavaType =
|
||||
new JsonJavaType<>( impliedJavaType,
|
||||
mutabilityPlan( typeConfiguration, impliedJavaType ),
|
||||
typeConfiguration );
|
||||
javaTypeRegistry.addDescriptor( jsonJavaType );
|
||||
return jsonJavaType;
|
||||
case SqlTypes.SQLXML:
|
||||
final JavaType<Object> xmlJavaType = new XmlJavaType<>(
|
||||
impliedJavaType,
|
||||
determinedMutabilityPlan,
|
||||
typeConfiguration
|
||||
);
|
||||
final JavaType<Object> xmlJavaType =
|
||||
new XmlJavaType<>( impliedJavaType,
|
||||
mutabilityPlan( typeConfiguration, impliedJavaType ),
|
||||
typeConfiguration );
|
||||
javaTypeRegistry.addDescriptor( xmlJavaType );
|
||||
return xmlJavaType;
|
||||
}
|
||||
}
|
||||
return javaTypeRegistry.resolveDescriptor( impliedJavaType );
|
||||
}
|
||||
return javaType;
|
||||
|
||||
private MutabilityPlan<Object> mutabilityPlan(
|
||||
TypeConfiguration typeConfiguration, java.lang.reflect.Type impliedJavaType) {
|
||||
final MutabilityPlan<Object> explicitMutabilityPlan = getExplicitMutabilityPlan();
|
||||
return explicitMutabilityPlan != null
|
||||
? explicitMutabilityPlan
|
||||
: RegistryHelper.INSTANCE.determineMutabilityPlan( impliedJavaType, typeConfiguration );
|
||||
}
|
||||
|
||||
private MutabilityPlan<Object> getExplicitMutabilityPlan() {
|
||||
return explicitMutabilityPlanAccess == null ? null
|
||||
: explicitMutabilityPlanAccess.apply( getTypeConfiguration() );
|
||||
}
|
||||
|
||||
private static Resolution<?> interpretExplicitlyNamedType(
|
||||
|
@ -1066,8 +1092,7 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
|
|||
: getUserTypeBean( explicitCustomType, properties ).getBeanInstance();
|
||||
|
||||
if ( typeInstance instanceof TypeConfigurationAware ) {
|
||||
final TypeConfigurationAware configurationAware = (TypeConfigurationAware) typeInstance;
|
||||
configurationAware.setTypeConfiguration( getTypeConfiguration() );
|
||||
( (TypeConfigurationAware) typeInstance ).setTypeConfiguration( getTypeConfiguration() );
|
||||
}
|
||||
|
||||
if ( typeInstance instanceof DynamicParameterizedType ) {
|
||||
|
@ -1097,11 +1122,12 @@ public class BasicValue extends SimpleValue implements JdbcTypeIndicators, Resol
|
|||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public void setTemporalPrecision(TemporalType temporalPrecision) {
|
||||
this.temporalPrecision = temporalPrecision;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override @SuppressWarnings("deprecation")
|
||||
public TemporalType getTemporalPrecision() {
|
||||
return temporalPrecision;
|
||||
}
|
||||
|
|
|
@ -68,59 +68,46 @@ public class BasicTypeRegistry implements Serializable {
|
|||
if ( typeReference == null ) {
|
||||
return null;
|
||||
}
|
||||
if ( !name.equals( typeReference.getName() ) ) {
|
||||
else if ( !name.equals( typeReference.getName() ) ) {
|
||||
final BasicType<?> basicType = typesByName.get( typeReference.getName() );
|
||||
if ( basicType != null ) {
|
||||
return basicType;
|
||||
}
|
||||
}
|
||||
final JavaType<Object> javaType = typeConfiguration.getJavaTypeRegistry().getDescriptor(
|
||||
typeReference.getBindableJavaType()
|
||||
);
|
||||
final JdbcType jdbcType = typeConfiguration.getJdbcTypeRegistry().getDescriptor(
|
||||
typeReference.getSqlTypeCode()
|
||||
);
|
||||
final BasicType<?> type;
|
||||
|
||||
return createBasicType( name, typeReference );
|
||||
}
|
||||
|
||||
private BasicType<?> createBasicType(String name, BasicTypeReference<?> typeReference) {
|
||||
final JavaType<Object> javaType =
|
||||
typeConfiguration.getJavaTypeRegistry()
|
||||
.getDescriptor( typeReference.getBindableJavaType() );
|
||||
final JdbcType jdbcType =
|
||||
typeConfiguration.getJdbcTypeRegistry()
|
||||
.getDescriptor( typeReference.getSqlTypeCode() );
|
||||
final BasicType<?> createdType = createBasicType( typeReference, javaType, jdbcType );
|
||||
primeRegistryEntry( createdType );
|
||||
typesByName.put( typeReference.getName(), createdType );
|
||||
typesByName.put( name, createdType );
|
||||
return createdType;
|
||||
}
|
||||
|
||||
private static BasicType<?> createBasicType(
|
||||
BasicTypeReference<?> typeReference, JavaType<Object> javaType, JdbcType jdbcType) {
|
||||
final String name = typeReference.getName();
|
||||
if ( typeReference.getConverter() == null ) {
|
||||
if ( typeReference.isForceImmutable() ) {
|
||||
type = new ImmutableNamedBasicTypeImpl<>(
|
||||
javaType,
|
||||
jdbcType,
|
||||
typeReference.getName()
|
||||
);
|
||||
return typeReference.isForceImmutable()
|
||||
? new ImmutableNamedBasicTypeImpl<>( javaType, jdbcType, name )
|
||||
: new NamedBasicTypeImpl<>( javaType, jdbcType, name );
|
||||
}
|
||||
else {
|
||||
type = new NamedBasicTypeImpl<>(
|
||||
javaType,
|
||||
jdbcType,
|
||||
typeReference.getName()
|
||||
);
|
||||
}
|
||||
}
|
||||
else {
|
||||
//noinspection unchecked
|
||||
final BasicValueConverter<Object, ?> converter = (BasicValueConverter<Object, ?>) typeReference.getConverter();
|
||||
final BasicValueConverter<?, ?> converter = typeReference.getConverter();
|
||||
assert javaType == converter.getDomainJavaType();
|
||||
if ( typeReference.isForceImmutable() ) {
|
||||
type = new CustomMutabilityConvertedBasicTypeImpl<>(
|
||||
typeReference.getName(),
|
||||
jdbcType,
|
||||
converter,
|
||||
ImmutableMutabilityPlan.instance()
|
||||
);
|
||||
return typeReference.isForceImmutable()
|
||||
? new CustomMutabilityConvertedBasicTypeImpl<>( name, jdbcType, converter,
|
||||
ImmutableMutabilityPlan.instance() )
|
||||
: new ConvertedBasicTypeImpl<>( name, jdbcType, converter );
|
||||
}
|
||||
else {
|
||||
type = new ConvertedBasicTypeImpl<>(
|
||||
typeReference.getName(),
|
||||
jdbcType,
|
||||
converter
|
||||
);
|
||||
}
|
||||
}
|
||||
primeRegistryEntry( type );
|
||||
typesByName.put( typeReference.getName(), type );
|
||||
typesByName.put( name, type );
|
||||
return type;
|
||||
}
|
||||
|
||||
public <J> BasicType<J> getRegisteredType(java.lang.reflect.Type javaType) {
|
||||
|
@ -144,10 +131,7 @@ public class BasicTypeRegistry implements Serializable {
|
|||
}
|
||||
|
||||
public <J> BasicType<J> resolve(JavaType<J> javaType, int sqlTypeCode) {
|
||||
return resolve(
|
||||
javaType,
|
||||
typeConfiguration.getJdbcTypeRegistry().getDescriptor( sqlTypeCode )
|
||||
);
|
||||
return resolve( javaType, typeConfiguration.getJdbcTypeRegistry().getDescriptor( sqlTypeCode ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -158,38 +142,36 @@ public class BasicTypeRegistry implements Serializable {
|
|||
return resolve(
|
||||
javaType,
|
||||
jdbcType,
|
||||
() -> {
|
||||
() -> resolvedType( javaType, jdbcType )
|
||||
);
|
||||
}
|
||||
|
||||
private <J> BasicType<J> resolvedType(JavaType<J> javaType, JdbcType jdbcType) {
|
||||
if ( javaType instanceof BasicPluralJavaType<?> && jdbcType instanceof ArrayJdbcType ) {
|
||||
//noinspection unchecked
|
||||
final BasicPluralJavaType<Object> pluralJavaType = (BasicPluralJavaType<Object>) javaType;
|
||||
final BasicType<Object> elementType = resolve(
|
||||
pluralJavaType.getElementJavaType(),
|
||||
( (ArrayJdbcType) jdbcType ).getElementJdbcType()
|
||||
);
|
||||
final BasicType<?> resolvedType = pluralJavaType.resolveType(
|
||||
return (BasicType<J>) resolvedType( (ArrayJdbcType) jdbcType, (BasicPluralJavaType<?>) javaType );
|
||||
}
|
||||
else {
|
||||
return new BasicTypeImpl<>( javaType, jdbcType );
|
||||
}
|
||||
}
|
||||
|
||||
private <E> BasicType<?> resolvedType(ArrayJdbcType arrayType, BasicPluralJavaType<E> castPluralJavaType) {
|
||||
final BasicType<?> resolvedType = castPluralJavaType.resolveType(
|
||||
typeConfiguration,
|
||||
typeConfiguration.getCurrentBaseSqlTypeIndicators().getDialect(),
|
||||
elementType,
|
||||
resolve( castPluralJavaType.getElementJavaType(), arrayType.getElementJdbcType() ),
|
||||
null,
|
||||
typeConfiguration.getCurrentBaseSqlTypeIndicators()
|
||||
);
|
||||
if ( resolvedType instanceof BasicPluralType<?,?> ) {
|
||||
register( resolvedType );
|
||||
}
|
||||
//noinspection unchecked
|
||||
return (BasicType<J>) resolvedType;
|
||||
}
|
||||
return new BasicTypeImpl<>( javaType, jdbcType );
|
||||
}
|
||||
);
|
||||
return resolvedType;
|
||||
}
|
||||
|
||||
public <J> BasicType<J> resolve(JavaType<J> javaType, JdbcType jdbcType, String baseTypeName) {
|
||||
return resolve(
|
||||
javaType,
|
||||
jdbcType,
|
||||
() -> new NamedBasicTypeImpl<>( javaType, jdbcType, baseTypeName )
|
||||
);
|
||||
return resolve( javaType, jdbcType, () -> new NamedBasicTypeImpl<>( javaType, jdbcType, baseTypeName ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -197,34 +179,48 @@ public class BasicTypeRegistry implements Serializable {
|
|||
* JdbcType combo or create (and register) one.
|
||||
*/
|
||||
public <J> BasicType<J> resolve(JavaType<J> javaType, JdbcType jdbcType, Supplier<BasicType<J>> creator) {
|
||||
final Map<JavaType<?>, BasicType<?>> typeByJavaTypeForJdbcType = registryValues.computeIfAbsent(
|
||||
jdbcType,
|
||||
key -> new ConcurrentHashMap<>()
|
||||
);
|
||||
|
||||
final BasicType<?> foundBasicType = typeByJavaTypeForJdbcType.get( javaType );
|
||||
if ( foundBasicType != null ) {
|
||||
final BasicType<?> registeredBasicType = registryForJdbcType( jdbcType ).get( javaType );
|
||||
//noinspection unchecked
|
||||
return (BasicType<J>) foundBasicType;
|
||||
return registeredBasicType != null
|
||||
? (BasicType<J>) registeredBasicType
|
||||
: createIfUnregistered( javaType, jdbcType, creator );
|
||||
}
|
||||
|
||||
private <J> BasicType<J> createIfUnregistered(
|
||||
JavaType<J> javaType,
|
||||
JdbcType jdbcType,
|
||||
Supplier<BasicType<J>> creator) {
|
||||
// Before simply creating the type, we try to find if there is a registered type for this java type,
|
||||
// and if so, if the jdbc type descriptor matches. Unless it does, we at least reuse the name
|
||||
final BasicType<J> registeredType = getRegisteredType( javaType.getJavaType() );
|
||||
if ( registeredType != null && registeredType.getJdbcType() == jdbcType && registeredType.getMappedJavaType() == javaType ) {
|
||||
if ( registeredTypeMatches( javaType, jdbcType, registeredType ) ) {
|
||||
return registeredType;
|
||||
}
|
||||
final BasicType<J> createdBasicType = creator.get();
|
||||
typeByJavaTypeForJdbcType.put( javaType, createdBasicType );
|
||||
else {
|
||||
final BasicType<J> createdType = creator.get();
|
||||
register( javaType, jdbcType, createdType );
|
||||
return createdType;
|
||||
}
|
||||
}
|
||||
|
||||
// if we are still building mappings, register this ad-hoc type
|
||||
// via a unique code. this is to support envers
|
||||
private static <J> boolean registeredTypeMatches(JavaType<J> javaType, JdbcType jdbcType, BasicType<J> registeredType) {
|
||||
return registeredType != null
|
||||
&& registeredType.getJdbcType() == jdbcType
|
||||
&& registeredType.getMappedJavaType() == javaType;
|
||||
}
|
||||
|
||||
private <J> void register(JavaType<J> javaType, JdbcType jdbcType, BasicType<J> createdType) {
|
||||
if ( createdType != null ) {
|
||||
registryForJdbcType( jdbcType ).put( javaType, createdType );
|
||||
// if we are still building mappings, register this adhoc
|
||||
// type via a unique code. (This is to support Envers.)
|
||||
try {
|
||||
typeConfiguration.getMetadataBuildingContext().getBootstrapContext()
|
||||
.registerAdHocBasicType( createdBasicType );
|
||||
.registerAdHocBasicType( createdType );
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
}
|
||||
return createdBasicType;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -260,16 +256,12 @@ public class BasicTypeRegistry implements Serializable {
|
|||
}
|
||||
|
||||
private void applyOrOverwriteEntry(BasicType<?> type) {
|
||||
final Map<JavaType<?>, BasicType<?>> typeByJavaTypeForJdbcType = registryValues.computeIfAbsent(
|
||||
type.getJdbcType(),
|
||||
jdbcType -> new ConcurrentHashMap<>()
|
||||
);
|
||||
|
||||
final BasicType<?> existing = typeByJavaTypeForJdbcType.put( type.getMappedJavaType(), type );
|
||||
final JdbcType jdbcType = type.getJdbcType();
|
||||
final BasicType<?> existing = registryForJdbcType( jdbcType ).put( type.getMappedJavaType(), type );
|
||||
if ( existing != null ) {
|
||||
LOG.debugf(
|
||||
"BasicTypeRegistry registration overwritten (%s + %s); previous =`%s`",
|
||||
type.getJdbcType().getFriendlyName(),
|
||||
jdbcType.getFriendlyName(),
|
||||
type.getJavaTypeDescriptor(),
|
||||
existing
|
||||
);
|
||||
|
@ -360,26 +352,25 @@ public class BasicTypeRegistry implements Serializable {
|
|||
}
|
||||
|
||||
private void primeRegistryEntry(BasicType<?> type) {
|
||||
final Map<JavaType<?>, BasicType<?>> typeByJavaTypeForJdbcType = registryValues.computeIfAbsent(
|
||||
type.getJdbcType(),
|
||||
jdbcType -> new ConcurrentHashMap<>()
|
||||
);
|
||||
|
||||
final BasicType<?> existing = typeByJavaTypeForJdbcType.get( type.getMappedJavaType() );
|
||||
|
||||
final JdbcType jdbcType = type.getJdbcType();
|
||||
final BasicType<?> existing = registryForJdbcType( jdbcType ).get( type.getMappedJavaType() );
|
||||
if ( existing != null ) {
|
||||
LOG.debugf(
|
||||
"Skipping registration of BasicType (%s + %s); still priming. existing = %s",
|
||||
type.getJdbcType().getFriendlyName(),
|
||||
jdbcType.getFriendlyName(),
|
||||
type.getJavaTypeDescriptor(),
|
||||
existing
|
||||
);
|
||||
}
|
||||
else {
|
||||
typeByJavaTypeForJdbcType.put( type.getMappedJavaType(), type );
|
||||
registryForJdbcType( jdbcType ).put( type.getMappedJavaType(), type );
|
||||
}
|
||||
}
|
||||
|
||||
private Map<JavaType<?>, BasicType<?>> registryForJdbcType(JdbcType jdbcType) {
|
||||
return registryValues.computeIfAbsent( jdbcType, key -> new ConcurrentHashMap<>() );
|
||||
}
|
||||
|
||||
private void applyRegistrationKeys(BasicType<?> type, String[] keys) {
|
||||
for ( String key : keys ) {
|
||||
// be safe...
|
||||
|
|
|
@ -19,7 +19,7 @@ import org.hibernate.type.spi.TypeConfiguration;
|
|||
* Descriptor for a basic plural Java type.
|
||||
* A basic plural type represents a type, that is mapped to a single column instead of multiple rows.
|
||||
* This is used for array or collection types, that are backed by e.g. SQL array or JSON/XML DDL types.
|
||||
*
|
||||
* <p>
|
||||
* The interface can be implemented by a plural java type e.g. {@link org.hibernate.type.descriptor.java.spi.BasicCollectionJavaType}
|
||||
* and provides access to the element java type, as well as a hook to resolve the {@link BasicType} based on the element {@link BasicType},
|
||||
* in order to gain enough information to implement storage and retrieval of the composite data type via JDBC.
|
||||
|
|
Loading…
Reference in New Issue