HHH-16027 add TypeContributions.contributeAttributeConverter()

This commit is contained in:
Gavin 2023-01-11 20:32:01 +01:00 committed by Gavin King
parent 7444b26db4
commit 2e84d51838
4 changed files with 52 additions and 9 deletions

View File

@ -287,16 +287,19 @@ public class MetadataBuilderImpl implements MetadataBuilderImplementor, TypeCont
}
@Override
@Deprecated
public void contributeType(BasicType<?> type) {
options.basicTypeRegistrations.add( new BasicTypeRegistration( type ) );
}
@Override
@Deprecated
public void contributeType(BasicType<?> type, String... keys) {
options.basicTypeRegistrations.add( new BasicTypeRegistration( type, keys ) );
}
@Override
@Deprecated
public void contributeType(UserType<?> type, String[] keys) {
options.basicTypeRegistrations.add( new BasicTypeRegistration( type, keys, getTypeConfiguration() ) );
}
@ -306,6 +309,13 @@ public class MetadataBuilderImpl implements MetadataBuilderImplementor, TypeCont
return bootstrapContext.getTypeConfiguration();
}
@Override
public void contributeAttributeConverter(Class<? extends AttributeConverter<?, ?>> converterClass) {
bootstrapContext.addAttributeConverterDescriptor(
new ClassBasedConverterDescriptor( converterClass, bootstrapContext.getClassmateContext() )
);
}
@Override
public MetadataBuilder applyCacheRegionDefinition(CacheRegionDefinition cacheRegionDefinition) {
this.bootstrapContext.addCacheRegionDefinition( cacheRegionDefinition );
@ -319,6 +329,7 @@ public class MetadataBuilderImpl implements MetadataBuilderImplementor, TypeCont
}
@Override
@Deprecated
public MetadataBuilder applySourceProcessOrdering(MetadataSourceType... sourceTypes) {
Collections.addAll( options.sourceProcessOrdering, sourceTypes );
return this;

View File

@ -6,6 +6,8 @@
*/
package org.hibernate.boot.model;
import jakarta.persistence.AttributeConverter;
import org.hibernate.Incubating;
import org.hibernate.type.BasicType;
import org.hibernate.type.CustomType;
import org.hibernate.type.StandardBasicTypeTemplate;
@ -21,11 +23,13 @@ import org.hibernate.usertype.UserType;
* {@link TypeConfiguration}, either by a {@link org.hibernate.dialect.Dialect}
* or by a {@link TypeContributor}.
*
* @see TypeContributor
*
* @author Steve Ebersole
* @see TypeContributor
*/
public interface TypeContributions {
/**
* The {@link TypeConfiguration} to contribute to
*/
TypeConfiguration getTypeConfiguration();
/**
@ -46,12 +50,23 @@ public interface TypeContributions {
/**
* Register a {@link UserType} as the implicit (auto-applied)
* type for values of type {@link UserType#returnedClass()}
* type for values of type {@link UserType#returnedClass()}.
*/
default <T> void contributeType(UserType<T> type) {
default void contributeType(UserType<?> type) {
contributeType( type, type.returnedClass().getName() );
}
/**
* Register an {@link AttributeConverter} class.
*
* @since 6.2
*/
@Incubating
default void contributeAttributeConverter(Class<? extends AttributeConverter<?, ?>> converterClass) {
// default implementation for backward compatibility
throw new UnsupportedOperationException();
}
/**
* @deprecated See discussion of {@code TypeContributor} in User Guide.
*/

View File

@ -11,9 +11,11 @@ import org.hibernate.Incubating;
import jakarta.persistence.AttributeConverter;
/**
* AttributeConverter registrations
* A registry for JPA {@linkplain AttributeConverter converters}.
*
* @author Gavin King
* @see AttributeConverter
* @since 6.2
*/
@Incubating
public interface ConverterRegistry {

View File

@ -15,6 +15,7 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import jakarta.persistence.AttributeConverter;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.internal.InFlightMetadataCollectorImpl;
import org.hibernate.boot.internal.MetadataBuildingContextRootImpl;
@ -23,6 +24,7 @@ import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping;
import org.hibernate.boot.jaxb.internal.MappingBinder;
import org.hibernate.boot.model.TypeContributions;
import org.hibernate.boot.model.TypeContributor;
import org.hibernate.boot.model.convert.spi.ConverterRegistry;
import org.hibernate.boot.model.process.internal.ManagedResourcesImpl;
import org.hibernate.boot.model.process.internal.ScanningCoordinator;
import org.hibernate.boot.model.relational.AuxiliaryDatabaseObject;
@ -154,7 +156,7 @@ public class MetadataBuildingProcess {
options
);
handleTypes( bootstrapContext, options );
handleTypes( bootstrapContext, options, metadataCollector );
final ClassLoaderService classLoaderService = options.getServiceRegistry().getService( ClassLoaderService.class );
@ -520,13 +522,26 @@ public class MetadataBuildingProcess {
// return new JandexInitManager( options.getJandexView(), classLoaderAccess, autoIndexMembers );
// }
private static void handleTypes(BootstrapContext bootstrapContext, MetadataBuildingOptions options) {
final ClassLoaderService classLoaderService = options.getServiceRegistry().getService( ClassLoaderService.class );
private static void handleTypes(
BootstrapContext bootstrapContext,
MetadataBuildingOptions options,
ConverterRegistry converterRegistry) {
final ClassLoaderService classLoaderService = options.getServiceRegistry().getService(ClassLoaderService.class);
final TypeConfiguration typeConfiguration = bootstrapContext.getTypeConfiguration();
final StandardServiceRegistry serviceRegistry = bootstrapContext.getServiceRegistry();
final JdbcTypeRegistry jdbcTypeRegistry = typeConfiguration.getJdbcTypeRegistry();
final TypeContributions typeContributions = () -> typeConfiguration;
final TypeContributions typeContributions = new TypeContributions() {
@Override
public TypeConfiguration getTypeConfiguration() {
return typeConfiguration;
}
@Override
public void contributeAttributeConverter(Class<? extends AttributeConverter<?, ?>> converterClass) {
converterRegistry.addAttributeConverter( converterClass );
}
};
// add Dialect contributed types
final Dialect dialect = options.getServiceRegistry().getService( JdbcServices.class ).getDialect();