HHH-18520 Upgrade to hibernate-models 0.9.0 + HHH-18521 Leverage hibernate-models ModelsConfiguration

This commit is contained in:
Andrea Boriero 2024-08-28 13:48:24 +02:00 committed by Steve Ebersole
parent 6e2ed7f1a0
commit 3c1b5c7a1d
15 changed files with 165 additions and 262 deletions

View File

@ -28,6 +28,7 @@ dependencies {
api jakartaLibs.jta api jakartaLibs.jta
implementation libs.hibernateModels implementation libs.hibernateModels
implementation libs.hibernateModelsJandex
implementation libs.jandex implementation libs.jandex
implementation libs.classmate implementation libs.classmate
implementation libs.byteBuddy implementation libs.byteBuddy
@ -65,6 +66,7 @@ dependencies {
} }
testImplementation "joda-time:joda-time:2.3" testImplementation "joda-time:joda-time:2.3"
testImplementation dbLibs.h2 testImplementation dbLibs.h2
testImplementation libs.hibernateModelsJandex
testRuntimeOnly libs.byteBuddy testRuntimeOnly libs.byteBuddy
testRuntimeOnly testLibs.weld testRuntimeOnly testLibs.weld

View File

@ -105,8 +105,8 @@ import org.hibernate.mapping.Table;
import org.hibernate.metamodel.CollectionClassification; import org.hibernate.metamodel.CollectionClassification;
import org.hibernate.metamodel.mapping.DiscriminatorType; import org.hibernate.metamodel.mapping.DiscriminatorType;
import org.hibernate.metamodel.spi.EmbeddableInstantiator; import org.hibernate.metamodel.spi.EmbeddableInstantiator;
import org.hibernate.models.internal.SourceModelBuildingContextImpl;
import org.hibernate.models.spi.ClassDetails; import org.hibernate.models.spi.ClassDetails;
import org.hibernate.models.spi.ModelsConfiguration;
import org.hibernate.models.spi.SourceModelBuildingContext; import org.hibernate.models.spi.SourceModelBuildingContext;
import org.hibernate.query.named.NamedObjectRepository; import org.hibernate.query.named.NamedObjectRepository;
import org.hibernate.query.sqm.function.SqmFunctionDescriptor; import org.hibernate.query.sqm.function.SqmFunctionDescriptor;
@ -228,11 +228,13 @@ public class InFlightMetadataCollectorImpl implements InFlightMetadataCollector,
private static SourceModelBuildingContext createModelBuildingContext(BootstrapContext bootstrapContext) { private static SourceModelBuildingContext createModelBuildingContext(BootstrapContext bootstrapContext) {
final ClassLoaderService classLoaderService = bootstrapContext.getServiceRegistry().getService( ClassLoaderService.class ); final ClassLoaderService classLoaderService = bootstrapContext.getServiceRegistry().getService( ClassLoaderService.class );
final ClassLoaderServiceLoading classLoading = new ClassLoaderServiceLoading( classLoaderService ); final ClassLoaderServiceLoading classLoading = new ClassLoaderServiceLoading( classLoaderService );
return new SourceModelBuildingContextImpl(
classLoading, final ModelsConfiguration modelsConfiguration = new ModelsConfiguration();
bootstrapContext.getJandexView(), modelsConfiguration.setClassLoading(classLoading);
ModelsHelper::preFillRegistries // modelsConfiguration.setExplicitContextProvider( );
); modelsConfiguration.configValue( "hibernate.models.jandex.index", bootstrapContext.getJandexView() );
modelsConfiguration.setRegistryPrimer( ModelsHelper::preFillRegistries );
return modelsConfiguration.bootstrap();
} }
@Override @Override

View File

@ -87,10 +87,8 @@ public class PropertyInferredData implements PropertyData {
if ( targetAnnotation != null ) { if ( targetAnnotation != null ) {
final String targetName = targetAnnotation.value(); final String targetName = targetAnnotation.value();
final SourceModelBuildingContext sourceModelBuildingContext = sourceModelContext; final SourceModelBuildingContext sourceModelBuildingContext = sourceModelContext;
final ClassDetails classDetails = sourceModelBuildingContext.getClassDetailsRegistry().resolveClassDetails( final ClassDetails classDetails = sourceModelBuildingContext.getClassDetailsRegistry()
targetName, .resolveClassDetails( targetName );
name -> new DynamicClassDetails( targetName, sourceModelBuildingContext )
);
return new ClassTypeDetailsImpl( classDetails, TypeDetails.Kind.CLASS ); return new ClassTypeDetailsImpl( classDetails, TypeDetails.Kind.CLASS );
} }
@ -119,10 +117,8 @@ public class PropertyInferredData implements PropertyData {
final org.hibernate.boot.internal.Target annotationUsage = propertyMember.getDirectAnnotationUsage( org.hibernate.boot.internal.Target.class ); final org.hibernate.boot.internal.Target annotationUsage = propertyMember.getDirectAnnotationUsage( org.hibernate.boot.internal.Target.class );
if ( annotationUsage != null ) { if ( annotationUsage != null ) {
final String targetName = annotationUsage.value(); final String targetName = annotationUsage.value();
final ClassDetails classDetails = sourceModelBuildingContext.getClassDetailsRegistry().resolveClassDetails( final ClassDetails classDetails = sourceModelBuildingContext.getClassDetailsRegistry()
targetName, .resolveClassDetails( targetName );
name -> new DynamicClassDetails( targetName, sourceModelBuildingContext )
);
return new ClassTypeDetailsImpl( classDetails, TypeDetails.Kind.CLASS ); return new ClassTypeDetailsImpl( classDetails, TypeDetails.Kind.CLASS );
} }

View File

@ -72,7 +72,8 @@ import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters; import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.mapping.Table; import org.hibernate.mapping.Table;
import org.hibernate.models.internal.jandex.JandexIndexerHelper; import org.hibernate.models.internal.MutableClassDetailsRegistry;
import org.hibernate.models.jandex.internal.JandexIndexerHelper;
import org.hibernate.models.spi.ClassDetails; import org.hibernate.models.spi.ClassDetails;
import org.hibernate.models.spi.ClassDetailsRegistry; import org.hibernate.models.spi.ClassDetailsRegistry;
import org.hibernate.models.spi.ClassLoading; import org.hibernate.models.spi.ClassLoading;
@ -458,7 +459,7 @@ public class MetadataBuildingProcess {
xmlProcessingResult.apply( xmlPreProcessingResult.getPersistenceUnitMetadata() ); xmlProcessingResult.apply( xmlPreProcessingResult.getPersistenceUnitMetadata() );
return new DomainModelSource( return new DomainModelSource(
classDetailsRegistry.makeImmutableCopy(), classDetailsRegistry,
jandexIndex, jandexIndex,
allKnownClassNames, allKnownClassNames,
modelCategorizationCollector.getGlobalRegistrations(), modelCategorizationCollector.getGlobalRegistrations(),
@ -516,40 +517,6 @@ public class MetadataBuildingProcess {
return CompositeIndex.create( suppliedJandexIndex, jandexIndexer.complete() ); return CompositeIndex.create( suppliedJandexIndex, jandexIndexer.complete() );
} }
//
// public static void preFillRegistries(RegistryPrimer.Contributions contributions, SourceModelBuildingContext buildingContext) {
// OrmAnnotationHelper.forEachOrmAnnotation( contributions::registerAnnotation );
//
// final IndexView jandexIndex = buildingContext.getJandexIndex();
// if ( jandexIndex == null ) {
// return;
// }
//
// final ClassDetailsRegistry classDetailsRegistry = buildingContext.getClassDetailsRegistry();
// final AnnotationDescriptorRegistry annotationDescriptorRegistry = buildingContext.getAnnotationDescriptorRegistry();
//
// for ( ClassInfo knownClass : jandexIndex.getKnownClasses() ) {
// final String className = knownClass.name().toString();
//
// if ( knownClass.isAnnotation() ) {
// // it is always safe to load the annotation classes - we will never be enhancing them
// //noinspection rawtypes
// final Class annotationClass = buildingContext
// .getClassLoading()
// .classForName( className );
// //noinspection unchecked
// annotationDescriptorRegistry.resolveDescriptor(
// annotationClass,
// (t) -> JdkBuilders.buildAnnotationDescriptor( annotationClass, buildingContext )
// );
// }
//
// classDetailsRegistry.resolveClassDetails(
// className,
// (name) -> new JandexClassDetails( knownClass, buildingContext )
// );
// }
// }
private static void processAdditionalMappingContributions( private static void processAdditionalMappingContributions(
InFlightMetadataCollectorImpl metadataCollector, InFlightMetadataCollectorImpl metadataCollector,
@ -628,6 +595,7 @@ public class MetadataBuildingProcess {
additionalClassDetails.add( classDetails ); additionalClassDetails.add( classDetails );
metadataCollector.getSourceModelBuildingContext() metadataCollector.getSourceModelBuildingContext()
.getClassDetailsRegistry() .getClassDetailsRegistry()
.as( MutableClassDetailsRegistry.class )
.addClassDetails( classDetails.getName(), classDetails ); .addClassDetails( classDetails.getName(), classDetails );
} }

View File

@ -7,6 +7,12 @@
package org.hibernate.boot.models.categorize.internal; package org.hibernate.boot.models.categorize.internal;
import java.net.URL; import java.net.URL;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;
import java.util.Set;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.models.spi.ClassLoading; import org.hibernate.models.spi.ClassLoading;
@ -37,4 +43,9 @@ public class ClassLoaderServiceLoading implements ClassLoading {
public URL locateResource(String resourceName) { public URL locateResource(String resourceName) {
return classLoaderService.locateResource( resourceName ); return classLoaderService.locateResource( resourceName );
} }
@Override
public <S> Collection<S> loadJavaServices(Class<S> serviceType) {
return classLoaderService.loadJavaServices( serviceType );
}
} }

View File

@ -31,8 +31,8 @@ import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException; import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
import org.hibernate.boot.spi.BootstrapContext; import org.hibernate.boot.spi.BootstrapContext;
import org.hibernate.boot.spi.MetadataBuildingOptions; import org.hibernate.boot.spi.MetadataBuildingOptions;
import org.hibernate.models.internal.SourceModelBuildingContextImpl; import org.hibernate.models.internal.BasicModelBuildingContextImpl;
import org.hibernate.models.internal.jandex.JandexIndexerHelper; import org.hibernate.models.jandex.internal.JandexIndexerHelper;
import org.hibernate.models.spi.AnnotationDescriptorRegistry; import org.hibernate.models.spi.AnnotationDescriptorRegistry;
import org.hibernate.models.spi.ClassDetails; import org.hibernate.models.spi.ClassDetails;
import org.hibernate.models.spi.ClassDetailsRegistry; import org.hibernate.models.spi.ClassDetailsRegistry;
@ -103,9 +103,8 @@ public class ManagedResourcesProcessor {
// At this point we know all managed class names across all sources. // At this point we know all managed class names across all sources.
// Resolve the Jandex Index and build the SourceModelBuildingContext. // Resolve the Jandex Index and build the SourceModelBuildingContext.
final IndexView jandexIndex = resolveJandexIndex( allKnownClassNames, bootstrapContext.getJandexView(), classLoading ); final IndexView jandexIndex = resolveJandexIndex( allKnownClassNames, bootstrapContext.getJandexView(), classLoading );
final SourceModelBuildingContextImpl sourceModelBuildingContext = new SourceModelBuildingContextImpl( final BasicModelBuildingContextImpl sourceModelBuildingContext = new BasicModelBuildingContextImpl(
classLoading, classLoading,
jandexIndex,
ModelsHelper::preFillRegistries ModelsHelper::preFillRegistries
); );
@ -174,16 +173,11 @@ public class ManagedResourcesProcessor {
// OUTPUTS: // OUTPUTS:
// - CategorizedDomainModel // - CategorizedDomainModel
final ClassDetailsRegistry classDetailsRegistryImmutable = classDetailsRegistry
.makeImmutableCopy();
final AnnotationDescriptorRegistry annotationDescriptorRegistryImmutable = descriptorRegistry
.makeImmutableCopy();
// Collect the entity hierarchies based on the set of `rootEntities` // Collect the entity hierarchies based on the set of `rootEntities`
final ModelCategorizationContextImpl mappingBuildingContext = new ModelCategorizationContextImpl( final ModelCategorizationContextImpl mappingBuildingContext = new ModelCategorizationContextImpl(
classDetailsRegistryImmutable, classDetailsRegistry,
annotationDescriptorRegistryImmutable, descriptorRegistry,
globalRegistrations globalRegistrations
); );
@ -212,8 +206,8 @@ public class ManagedResourcesProcessor {
return modelCategorizationCollector.createResult( return modelCategorizationCollector.createResult(
entityHierarchies, entityHierarchies,
xmlPreProcessingResult.getPersistenceUnitMetadata(), xmlPreProcessingResult.getPersistenceUnitMetadata(),
classDetailsRegistryImmutable, classDetailsRegistry,
annotationDescriptorRegistryImmutable descriptorRegistry
); );
} }

View File

@ -7,8 +7,8 @@
package org.hibernate.boot.models.internal; package org.hibernate.boot.models.internal;
import org.hibernate.annotations.TenantId; import org.hibernate.annotations.TenantId;
import org.hibernate.models.internal.jandex.JandexClassDetails;
import org.hibernate.models.internal.jdk.JdkBuilders; import org.hibernate.models.internal.jdk.JdkBuilders;
import org.hibernate.models.jandex.spi.JandexModelBuildingContext;
import org.hibernate.models.spi.AnnotationDescriptorRegistry; import org.hibernate.models.spi.AnnotationDescriptorRegistry;
import org.hibernate.models.spi.ClassDetailsRegistry; import org.hibernate.models.spi.ClassDetailsRegistry;
import org.hibernate.models.spi.RegistryPrimer; import org.hibernate.models.spi.RegistryPrimer;
@ -26,7 +26,8 @@ public class ModelsHelper {
buildingContext.getAnnotationDescriptorRegistry().getDescriptor( TenantId.class ); buildingContext.getAnnotationDescriptorRegistry().getDescriptor( TenantId.class );
final IndexView jandexIndex = buildingContext.getJandexIndex(); if ( buildingContext instanceof JandexModelBuildingContext ) {
final IndexView jandexIndex = buildingContext.as( JandexModelBuildingContext.class ).getJandexIndex();
if ( jandexIndex == null ) { if ( jandexIndex == null ) {
return; return;
} }
@ -50,10 +51,8 @@ public class ModelsHelper {
); );
} }
classDetailsRegistry.resolveClassDetails( classDetailsRegistry.resolveClassDetails( className );
className, }
(name) -> new JandexClassDetails( knownClass, buildingContext )
);
} }
} }
} }

View File

@ -15,7 +15,6 @@ import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.hibernate.boot.models.DialectOverrideAnnotations; import org.hibernate.boot.models.DialectOverrideAnnotations;
@ -23,13 +22,13 @@ import org.hibernate.boot.models.HibernateAnnotations;
import org.hibernate.boot.models.JpaAnnotations; import org.hibernate.boot.models.JpaAnnotations;
import org.hibernate.boot.models.XmlAnnotations; import org.hibernate.boot.models.XmlAnnotations;
import org.hibernate.models.AnnotationAccessException; import org.hibernate.models.AnnotationAccessException;
import org.hibernate.models.jandex.spi.JandexModelBuildingContext;
import org.hibernate.models.jandex.spi.JandexValueExtractor;
import org.hibernate.models.spi.AnnotationDescriptor; import org.hibernate.models.spi.AnnotationDescriptor;
import org.hibernate.models.spi.AttributeDescriptor; import org.hibernate.models.spi.AttributeDescriptor;
import org.hibernate.models.spi.JandexValueExtractor;
import org.hibernate.models.spi.SourceModelBuildingContext; import org.hibernate.models.spi.SourceModelBuildingContext;
import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
@ -76,7 +75,8 @@ public class OrmAnnotationHelper {
} }
public static <V> V extractJandexValue(AnnotationInstance jandexAnnotation, AttributeDescriptor<V> attributeDescriptor, SourceModelBuildingContext modelContext) { public static <V> V extractJandexValue(AnnotationInstance jandexAnnotation, AttributeDescriptor<V> attributeDescriptor, SourceModelBuildingContext modelContext) {
final JandexValueExtractor<V> extractor = attributeDescriptor.getTypeDescriptor().createJandexValueExtractor( modelContext ); final JandexValueExtractor<V> extractor = modelContext.as( JandexModelBuildingContext.class )
.getJandexValueExtractor( attributeDescriptor.getTypeDescriptor() );
return extractor.extractValue( jandexAnnotation, attributeDescriptor, modelContext ); return extractor.extractValue( jandexAnnotation, attributeDescriptor, modelContext );
// final AnnotationValue value = jandexAnnotation.value( attributeDescriptor.getName() ); // final AnnotationValue value = jandexAnnotation.value( attributeDescriptor.getName() );
// return attributeDescriptor // return attributeDescriptor

View File

@ -44,9 +44,10 @@ import org.hibernate.models.ModelsException;
import org.hibernate.models.internal.ClassTypeDetailsImpl; import org.hibernate.models.internal.ClassTypeDetailsImpl;
import org.hibernate.models.internal.ModelsClassLogging; import org.hibernate.models.internal.ModelsClassLogging;
import org.hibernate.models.internal.ModifierUtils; import org.hibernate.models.internal.ModifierUtils;
import org.hibernate.models.internal.RenderingCollectorImpl;
import org.hibernate.models.internal.dynamic.DynamicClassDetails; import org.hibernate.models.internal.dynamic.DynamicClassDetails;
import org.hibernate.models.internal.dynamic.DynamicFieldDetails; import org.hibernate.models.internal.dynamic.DynamicFieldDetails;
import org.hibernate.models.rendering.internal.RenderingTargetCollectingImpl;
import org.hibernate.models.rendering.internal.SimpleRenderer;
import org.hibernate.models.spi.ClassDetails; import org.hibernate.models.spi.ClassDetails;
import org.hibernate.models.spi.ClassDetailsRegistry; import org.hibernate.models.spi.ClassDetailsRegistry;
import org.hibernate.models.spi.FieldDetails; import org.hibernate.models.spi.FieldDetails;
@ -99,17 +100,7 @@ public class ManagedTypeProcessor {
memberAdjuster = ManagedTypeProcessor::adjustDynamicTypeMember; memberAdjuster = ManagedTypeProcessor::adjustDynamicTypeMember;
classAccessType = AccessType.FIELD; classAccessType = AccessType.FIELD;
classDetails = (MutableClassDetails) classDetailsRegistry.resolveClassDetails( classDetails = (MutableClassDetails) classDetailsRegistry.resolveClassDetails( jaxbEntity.getName() );
jaxbEntity.getName(),
(name) -> new DynamicClassDetails(
jaxbEntity.getName(),
null,
false,
null,
null,
xmlDocumentContext.getModelBuildingContext()
)
);
prepareDynamicClass( classDetails, jaxbEntity, xmlDocumentContext ); prepareDynamicClass( classDetails, jaxbEntity, xmlDocumentContext );
} }
@ -625,9 +616,10 @@ public class ManagedTypeProcessor {
return; return;
} }
final RenderingCollectorImpl renderingCollector = new RenderingCollectorImpl(); final RenderingTargetCollectingImpl collectingTarget = new RenderingTargetCollectingImpl();
classDetails.render( renderingCollector, xmlDocumentContext.getModelBuildingContext() ); final SimpleRenderer renderer = new SimpleRenderer( collectingTarget );
XML_PROCESS_LOGGER.debugf( "Class annotations from XML for %s:\n%s", classDetails.getName(), renderingCollector.toString() ); renderer.renderClass( classDetails, xmlDocumentContext.getModelBuildingContext() );
XML_PROCESS_LOGGER.debugf( "Class annotations from XML for %s:\n%s", classDetails.getName(), renderer.toString() );
} }
private static void applyAccessAnnotation( private static void applyAccessAnnotation(
@ -943,7 +935,7 @@ public class ManagedTypeProcessor {
} }
// no class == dynamic... // no class == dynamic...
classDetails = (MutableClassDetails) classDetailsRegistry classDetails = (MutableClassDetails) classDetailsRegistry
.resolveClassDetails( jaxbEmbeddable.getName(), DynamicClassDetails::new ); .resolveClassDetails( jaxbEmbeddable.getName() );
classAccessType = AccessType.FIELD; classAccessType = AccessType.FIELD;
memberAdjuster = ManagedTypeProcessor::adjustDynamicTypeMember; memberAdjuster = ManagedTypeProcessor::adjustDynamicTypeMember;

View File

@ -137,10 +137,8 @@ public interface XmlDocumentContext {
// <embedded/>, <embedded-id/> // <embedded/>, <embedded-id/>
final String target = jaxbEmbeddedMapping.getTarget(); final String target = jaxbEmbeddedMapping.getTarget();
if ( isNotEmpty( target ) ) { if ( isNotEmpty( target ) ) {
return (MutableClassDetails) getModelBuildingContext().getClassDetailsRegistry().resolveClassDetails( return (MutableClassDetails) getModelBuildingContext().getClassDetailsRegistry()
target, .resolveClassDetails( target );
(name) -> new DynamicClassDetails( target, getModelBuildingContext() )
);
} }
// fall through to exception // fall through to exception
} }
@ -148,17 +146,8 @@ public interface XmlDocumentContext {
if ( jaxbPersistentAttribute instanceof JaxbAssociationAttribute jaxbAssociationAttribute ) { if ( jaxbPersistentAttribute instanceof JaxbAssociationAttribute jaxbAssociationAttribute ) {
final String target = jaxbAssociationAttribute.getTargetEntity(); final String target = jaxbAssociationAttribute.getTargetEntity();
if ( isNotEmpty( target ) ) { if ( isNotEmpty( target ) ) {
return (MutableClassDetails) getModelBuildingContext().getClassDetailsRegistry().resolveClassDetails( return (MutableClassDetails) getModelBuildingContext().getClassDetailsRegistry()
target, .resolveClassDetails( target );
(name) -> new DynamicClassDetails(
target,
null,
false,
null,
null,
getModelBuildingContext()
)
);
} }
// fall through to exception // fall through to exception
} }

View File

@ -20,7 +20,7 @@ import org.hibernate.boot.models.xml.spi.XmlProcessingResult;
import org.hibernate.boot.models.xml.spi.XmlProcessor; import org.hibernate.boot.models.xml.spi.XmlProcessor;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.BootstrapContext; import org.hibernate.boot.spi.BootstrapContext;
import org.hibernate.models.internal.SourceModelBuildingContextImpl; import org.hibernate.models.internal.BasicModelBuildingContextImpl;
import org.hibernate.models.spi.ClassDetails; import org.hibernate.models.spi.ClassDetails;
import org.hibernate.models.spi.FieldDetails; import org.hibernate.models.spi.FieldDetails;
import org.hibernate.models.spi.MemberDetails; import org.hibernate.models.spi.MemberDetails;
@ -91,9 +91,8 @@ public abstract class Ejb3XmlTestCase extends BaseUnitTestCase {
persistenceUnitMetadata persistenceUnitMetadata
); );
final SourceModelBuildingContext modelBuildingContext = new SourceModelBuildingContextImpl( final SourceModelBuildingContext modelBuildingContext = new BasicModelBuildingContextImpl(
SIMPLE_CLASS_LOADING, SIMPLE_CLASS_LOADING,
null,
(contributions, inFlightContext) -> { (contributions, inFlightContext) -> {
OrmAnnotationHelper.forEachOrmAnnotation( contributions::registerAnnotation ); OrmAnnotationHelper.forEachOrmAnnotation( contributions::registerAnnotation );
} }

View File

@ -52,17 +52,15 @@ import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.MappingSettings; import org.hibernate.cfg.MappingSettings;
import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.models.internal.AnnotationDescriptorRegistryStandard;
import org.hibernate.models.internal.BaseLineJavaTypes; import org.hibernate.models.internal.BaseLineJavaTypes;
import org.hibernate.models.internal.SourceModelBuildingContextImpl; import org.hibernate.models.internal.BasicModelBuildingContextImpl;
import org.hibernate.models.internal.jandex.JandexBuilders;
import org.hibernate.models.internal.jandex.JandexIndexerHelper;
import org.hibernate.models.internal.jdk.JdkBuilders; import org.hibernate.models.internal.jdk.JdkBuilders;
import org.hibernate.models.jandex.internal.JandexIndexerHelper;
import org.hibernate.models.jandex.internal.JandexModelBuildingContextImpl;
import org.hibernate.models.spi.ClassDetails; import org.hibernate.models.spi.ClassDetails;
import org.hibernate.models.spi.ClassDetailsRegistry; import org.hibernate.models.spi.ClassDetailsRegistry;
import org.hibernate.models.spi.ClassLoading; import org.hibernate.models.spi.ClassLoading;
import org.hibernate.models.spi.SourceModelBuildingContext; import org.hibernate.models.spi.SourceModelBuildingContext;
import org.hibernate.service.ServiceRegistry;
import org.jboss.jandex.ClassInfo; import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.Index; import org.jboss.jandex.Index;
@ -87,49 +85,50 @@ public class SourceModelTestHelper {
return createBuildingContext( jandexIndex, modelClasses ); return createBuildingContext( jandexIndex, modelClasses );
} }
public static SourceModelBuildingContextImpl createBuildingContext(Index jandexIndex, Class<?>... modelClasses) { public static SourceModelBuildingContext createBuildingContext(Index jandexIndex, Class<?>... modelClasses) {
return createBuildingContext( jandexIndex, SIMPLE_CLASS_LOADING, modelClasses ); return createBuildingContext( jandexIndex, SIMPLE_CLASS_LOADING, modelClasses );
} }
public static SourceModelBuildingContextImpl createBuildingContext( public static SourceModelBuildingContext createBuildingContext(
Index jandexIndex, Index jandexIndex,
ClassLoading classLoadingAccess, ClassLoading classLoadingAccess,
Class<?>... modelClasses) { Class<?>... modelClasses) {
final SourceModelBuildingContextImpl buildingContext = new SourceModelBuildingContextImpl( final SourceModelBuildingContext ctx;
if ( jandexIndex == null ) {
ctx = new BasicModelBuildingContextImpl(
classLoadingAccess, classLoadingAccess,
jandexIndex,
(contributions, buildingContext1) -> OrmAnnotationHelper.forEachOrmAnnotation( contributions::registerAnnotation ) (contributions, buildingContext1) -> OrmAnnotationHelper.forEachOrmAnnotation( contributions::registerAnnotation )
); );
final ClassDetailsRegistry classDetailsRegistry = buildingContext.getClassDetailsRegistry(); }
final AnnotationDescriptorRegistryStandard annotationDescriptorRegistry = (AnnotationDescriptorRegistryStandard) buildingContext.getAnnotationDescriptorRegistry(); else {
ctx = new JandexModelBuildingContextImpl(
for ( ClassInfo knownClass : jandexIndex.getKnownClasses() ) { jandexIndex,
// if ( knownClass.simpleName().endsWith( "package-info" ) ) { classLoadingAccess,
// new PackageDetailsImpl( knownClass, buildingContext ); (contributions, buildingContext1) -> OrmAnnotationHelper.forEachOrmAnnotation( contributions::registerAnnotation )
// continue;
// }
classDetailsRegistry.resolveClassDetails(
knownClass.name().toString(),
JandexBuilders.DEFAULT_BUILDER
); );
for ( ClassInfo knownClass : jandexIndex.getKnownClasses() ) {
ctx.getClassDetailsRegistry().resolveClassDetails( knownClass.name().toString() );
if ( knownClass.isAnnotation() ) { if ( knownClass.isAnnotation() ) {
final Class<? extends Annotation> annotationClass = buildingContext final Class<? extends Annotation> annotationClass = classLoadingAccess.classForName( knownClass.name().toString() );
.getClassLoading() ctx.getAnnotationDescriptorRegistry().resolveDescriptor(
.classForName( knownClass.name().toString() ); annotationClass,
annotationDescriptorRegistry.resolveDescriptor( annotationClass, annotationType -> JdkBuilders.buildAnnotationDescriptor( annotationType -> JdkBuilders.buildAnnotationDescriptor(
annotationType, annotationType,
buildingContext ctx
) ); )
);
}
} }
} }
for ( int i = 0; i < modelClasses.length; i++ ) { for ( int i = 0; i < modelClasses.length; i++ ) {
classDetailsRegistry.resolveClassDetails( modelClasses[i].getName() ); ctx.getClassDetailsRegistry().resolveClassDetails( modelClasses[i].getName() );
} }
return buildingContext; return ctx;
} }
public static Index buildJandexIndex(Class<?>... modelClasses) { public static Index buildJandexIndex(Class<?>... modelClasses) {
@ -275,11 +274,21 @@ public class SourceModelTestHelper {
? buildJandexIndex( classLoading, allKnownClassNames ) ? buildJandexIndex( classLoading, allKnownClassNames )
: null; : null;
final SourceModelBuildingContextImpl sourceModelBuildingContext = new SourceModelBuildingContextImpl( final SourceModelBuildingContext sourceModelBuildingContext;
if ( jandexIndex == null ) {
sourceModelBuildingContext = new BasicModelBuildingContextImpl(
classLoading, classLoading,
jandexIndex,
ModelsHelper::preFillRegistries ModelsHelper::preFillRegistries
); );
}
else {
sourceModelBuildingContext = new JandexModelBuildingContextImpl(
jandexIndex,
classLoading,
ModelsHelper::preFillRegistries
);
}
final RootMappingDefaults rootMappingDefaults = new RootMappingDefaults( final RootMappingDefaults rootMappingDefaults = new RootMappingDefaults(
metadataBuildingOptions.getMappingDefaults(), metadataBuildingOptions.getMappingDefaults(),

View File

@ -7,7 +7,7 @@
package org.hibernate.orm.test.boot.models; package org.hibernate.orm.test.boot.models;
import org.hibernate.models.AnnotationAccessException; import org.hibernate.models.AnnotationAccessException;
import org.hibernate.models.internal.jandex.JandexClassDetails; import org.hibernate.models.jandex.internal.JandexClassDetails;
import org.hibernate.models.spi.AnnotationDescriptor; import org.hibernate.models.spi.AnnotationDescriptor;
import org.hibernate.models.spi.ClassDetails; import org.hibernate.models.spi.ClassDetails;
import org.hibernate.models.spi.FieldDetails; import org.hibernate.models.spi.FieldDetails;

View File

@ -11,20 +11,13 @@ import java.io.InputStream;
import java.util.List; import java.util.List;
import org.hibernate.boot.ResourceStreamLocator; import org.hibernate.boot.ResourceStreamLocator;
import org.hibernate.boot.models.HibernateAnnotations;
import org.hibernate.boot.models.JpaAnnotations;
import org.hibernate.boot.models.annotations.internal.EntityJpaAnnotation;
import org.hibernate.boot.spi.AdditionalMappingContributions; import org.hibernate.boot.spi.AdditionalMappingContributions;
import org.hibernate.boot.spi.AdditionalMappingContributor; import org.hibernate.boot.spi.AdditionalMappingContributor;
import org.hibernate.boot.spi.InFlightMetadataCollector; import org.hibernate.boot.spi.InFlightMetadataCollector;
import org.hibernate.boot.spi.MetadataBuildingContext; import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.mapping.PersistentClass; import org.hibernate.mapping.PersistentClass;
import org.hibernate.models.internal.dynamic.DynamicClassDetails;
import org.hibernate.models.internal.dynamic.DynamicFieldDetails;
import org.hibernate.models.internal.jdk.JdkClassDetails;
import org.hibernate.models.spi.ClassDetails; import org.hibernate.models.spi.ClassDetails;
import org.hibernate.models.spi.ClassDetailsRegistry; import org.hibernate.models.spi.ClassDetailsRegistry;
import org.hibernate.models.spi.MutableMemberDetails;
import org.hibernate.testing.orm.junit.BootstrapServiceRegistry; import org.hibernate.testing.orm.junit.BootstrapServiceRegistry;
import org.hibernate.testing.orm.junit.BootstrapServiceRegistry.JavaService; import org.hibernate.testing.orm.junit.BootstrapServiceRegistry.JavaService;
@ -43,7 +36,6 @@ import static org.assertj.core.api.Assertions.assertThat;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
*
* @implNote hibernate-envers is already a full testing of contributing a {@code hbm.xml} * @implNote hibernate-envers is already a full testing of contributing a {@code hbm.xml}
* document; so we skip that here until if/when we transition it to use a better approach * document; so we skip that here until if/when we transition it to use a better approach
*/ */
@ -105,13 +97,17 @@ public class AdditionalMappingContributorTests {
@DomainModel @DomainModel
@SessionFactory @SessionFactory
@SuppressWarnings("JUnitMalformedDeclaration") @SuppressWarnings("JUnitMalformedDeclaration")
void verifyJdkClassDetailsContributions(DomainModelScope domainModelScope, SessionFactoryScope sessionFactoryScope) { void verifyJdkClassDetailsContributions(
final PersistentClass entity4Binding = domainModelScope.getDomainModel().getEntityBinding( Entity4.class.getName() ); DomainModelScope domainModelScope,
SessionFactoryScope sessionFactoryScope) {
final PersistentClass entity4Binding = domainModelScope.getDomainModel()
.getEntityBinding( Entity4.class.getName() );
assertThat( entity4Binding ).isNotNull(); assertThat( entity4Binding ).isNotNull();
assertThat( entity4Binding.getIdentifierProperty() ).isNotNull(); assertThat( entity4Binding.getIdentifierProperty() ).isNotNull();
assertThat( entity4Binding.getProperties() ).hasSize( 1 ); assertThat( entity4Binding.getProperties() ).hasSize( 1 );
final PersistentClass entity5Binding = domainModelScope.getDomainModel().getEntityBinding( Entity5.class.getName() ); final PersistentClass entity5Binding = domainModelScope.getDomainModel()
.getEntityBinding( Entity5.class.getName() );
assertThat( entity5Binding ).isNotNull(); assertThat( entity5Binding ).isNotNull();
assertThat( entity5Binding.getIdentifierProperty() ).isNotNull(); assertThat( entity5Binding.getIdentifierProperty() ).isNotNull();
assertThat( entity5Binding.getProperties() ).hasSize( 1 ); assertThat( entity5Binding.getProperties() ).hasSize( 1 );
@ -137,7 +133,9 @@ public class AdditionalMappingContributorTests {
@DomainModel @DomainModel
@SessionFactory @SessionFactory
@SuppressWarnings("JUnitMalformedDeclaration") @SuppressWarnings("JUnitMalformedDeclaration")
void verifyDynamicClassDetailsContributions(DomainModelScope domainModelScope, SessionFactoryScope sessionFactoryScope) { void verifyDynamicClassDetailsContributions(
DomainModelScope domainModelScope,
SessionFactoryScope sessionFactoryScope) {
final PersistentClass entity6Binding = domainModelScope.getDomainModel().getEntityBinding( "Entity6" ); final PersistentClass entity6Binding = domainModelScope.getDomainModel().getEntityBinding( "Entity6" );
assertThat( entity6Binding ).isNotNull(); assertThat( entity6Binding ).isNotNull();
assertThat( entity6Binding.getIdentifierProperty() ).isNotNull(); assertThat( entity6Binding.getIdentifierProperty() ).isNotNull();
@ -277,7 +275,8 @@ public class AdditionalMappingContributorTests {
InFlightMetadataCollector metadata, InFlightMetadataCollector metadata,
ResourceStreamLocator resourceStreamLocator, ResourceStreamLocator resourceStreamLocator,
MetadataBuildingContext buildingContext) { MetadataBuildingContext buildingContext) {
try ( final InputStream stream = resourceStreamLocator.locateResourceStream( "mappings/intg/contributed-mapping.xml" ) ) { try (final InputStream stream = resourceStreamLocator.locateResourceStream(
"mappings/intg/contributed-mapping.xml" )) {
contributions.contributeBinding( stream ); contributions.contributeBinding( stream );
} }
catch (IOException e) { catch (IOException e) {
@ -297,50 +296,24 @@ public class AdditionalMappingContributorTests {
.getSourceModelBuildingContext() .getSourceModelBuildingContext()
.getClassDetailsRegistry(); .getClassDetailsRegistry();
contributeEntity4Details( contributions, buildingContext, classDetailsRegistry ); contributeEntity4Details( contributions, classDetailsRegistry );
contributeEntity5Details( contributions, buildingContext, classDetailsRegistry ); contributeEntity5Details( contributions, classDetailsRegistry );
} }
private static void contributeEntity4Details( private static void contributeEntity4Details(
AdditionalMappingContributions contributions, AdditionalMappingContributions contributions,
MetadataBuildingContext buildingContext,
ClassDetailsRegistry classDetailsRegistry) { ClassDetailsRegistry classDetailsRegistry) {
final ClassDetails entity4Details = classDetailsRegistry.resolveClassDetails( final ClassDetails entity4Details = classDetailsRegistry.resolveClassDetails(
Entity4.class.getName(), Entity4.class.getName()
(name, modelBuildingContext) -> {
assertThat( name ).isEqualTo( Entity4.class.getName() );
assertThat( modelBuildingContext ).isSameAs( buildingContext.getMetadataCollector().getSourceModelBuildingContext() );
return new JdkClassDetails( Entity4.class, modelBuildingContext );
}
); );
contributions.contributeManagedClass( entity4Details ); contributions.contributeManagedClass( entity4Details );
} }
private static void contributeEntity5Details( private static void contributeEntity5Details(
AdditionalMappingContributions contributions, AdditionalMappingContributions contributions,
MetadataBuildingContext buildingContext,
ClassDetailsRegistry classDetailsRegistry) { ClassDetailsRegistry classDetailsRegistry) {
final ClassDetails entity5Details = classDetailsRegistry.resolveClassDetails( final ClassDetails entity5Details = classDetailsRegistry.resolveClassDetails(
Entity5.class.getName(), Entity5.class.getName()
(name, modelBuildingContext) -> {
assertThat( name ).isEqualTo( Entity5.class.getName() );
assertThat( modelBuildingContext ).isSameAs( buildingContext.getMetadataCollector().getSourceModelBuildingContext() );
final JdkClassDetails jdkClassDetails = new JdkClassDetails(
Entity5.class,
modelBuildingContext
);
final EntityJpaAnnotation entityUsage = (EntityJpaAnnotation) jdkClassDetails.applyAnnotationUsage(
JpaAnnotations.ENTITY,
modelBuildingContext
);
entityUsage.name( "___Entity5___" );
final MutableMemberDetails idField = (MutableMemberDetails) jdkClassDetails.findFieldByName( "id" );
idField.applyAnnotationUsage( JpaAnnotations.ID, modelBuildingContext );
return jdkClassDetails;
}
); );
contributions.contributeManagedClass( entity5Details ); contributions.contributeManagedClass( entity5Details );
} }
@ -356,46 +329,14 @@ public class AdditionalMappingContributorTests {
final ClassDetailsRegistry classDetailsRegistry = buildingContext.getMetadataCollector() final ClassDetailsRegistry classDetailsRegistry = buildingContext.getMetadataCollector()
.getSourceModelBuildingContext() .getSourceModelBuildingContext()
.getClassDetailsRegistry(); .getClassDetailsRegistry();
contributeEntity6Details( contributions, buildingContext, classDetailsRegistry ); contributeEntity6Details( contributions, classDetailsRegistry );
} }
private void contributeEntity6Details( private void contributeEntity6Details(
AdditionalMappingContributions contributions, AdditionalMappingContributions contributions,
MetadataBuildingContext buildingContext,
ClassDetailsRegistry classDetailsRegistry) { ClassDetailsRegistry classDetailsRegistry) {
final ClassDetails entity6Details = classDetailsRegistry.resolveClassDetails( final ClassDetails entity6Details = classDetailsRegistry.resolveClassDetails(
"Entity6", "Entity6"
(name, modelBuildingContext) -> {
assertThat( name ).isEqualTo( "Entity6" );
assertThat( modelBuildingContext ).isSameAs( buildingContext.getMetadataCollector().getSourceModelBuildingContext() );
final DynamicClassDetails classDetails = new DynamicClassDetails( "Entity6", modelBuildingContext );
final EntityJpaAnnotation entityUsage = (EntityJpaAnnotation) classDetails.applyAnnotationUsage(
JpaAnnotations.ENTITY,
modelBuildingContext
);
entityUsage.name( "Entity6" );
final DynamicFieldDetails idMember = classDetails.applyAttribute(
"id",
classDetailsRegistry.resolveClassDetails( Integer.class.getName() ),
false,
false,
modelBuildingContext
);
idMember.applyAnnotationUsage( JpaAnnotations.ID, modelBuildingContext );
final DynamicFieldDetails nameMember = classDetails.applyAttribute(
"name",
classDetailsRegistry.resolveClassDetails( String.class.getName() ),
false,
false,
modelBuildingContext
);
nameMember.applyAnnotationUsage( HibernateAnnotations.NATIONALIZED, modelBuildingContext );
return classDetails;
}
); );
contributions.contributeManagedClass( entity6Details ); contributions.contributeManagedClass( entity6Details );
} }

View File

@ -76,7 +76,7 @@ dependencyResolutionManagement {
def byteBuddyVersion = version "byteBuddy", "1.14.18" def byteBuddyVersion = version "byteBuddy", "1.14.18"
def classmateVersion = version "classmate", "1.5.1" def classmateVersion = version "classmate", "1.5.1"
def geolatteVersion = version "geolatte", "1.9.1" def geolatteVersion = version "geolatte", "1.9.1"
def hibernateModelsVersion = version "hibernateModels", "0.8.6" def hibernateModelsVersion = version "hibernateModels", "0.9.0"
def jandexVersion = version "jandex", "3.2.0" def jandexVersion = version "jandex", "3.2.0"
def jacksonVersion = version "jackson", "2.17.0" def jacksonVersion = version "jackson", "2.17.0"
def jbossLoggingVersion = version "jbossLogging", "3.6.0.Final" def jbossLoggingVersion = version "jbossLogging", "3.6.0.Final"
@ -107,6 +107,7 @@ dependencyResolutionManagement {
library( "loggingProcessor", "org.jboss.logging", "jboss-logging-processor" ).versionRef( jbossLoggingToolVersion ) library( "loggingProcessor", "org.jboss.logging", "jboss-logging-processor" ).versionRef( jbossLoggingToolVersion )
library( "hibernateModels", "org.hibernate.models", "hibernate-models" ).versionRef( hibernateModelsVersion ) library( "hibernateModels", "org.hibernate.models", "hibernate-models" ).versionRef( hibernateModelsVersion )
library( "hibernateModelsJandex", "org.hibernate.models", "hibernate-models-jandex" ).versionRef( hibernateModelsVersion )
library( "jandex", "io.smallrye", "jandex" ).versionRef( jandexVersion ) library( "jandex", "io.smallrye", "jandex" ).versionRef( jandexVersion )
library( "classmate", "com.fasterxml", "classmate" ).versionRef( classmateVersion ) library( "classmate", "com.fasterxml", "classmate" ).versionRef( classmateVersion )