From b63af21c772ed01d2c4a9ef3f4274975b1642299 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Wed, 13 Mar 2024 14:52:21 -0500 Subject: [PATCH] HHH-17460 - Ongoing JPA 32 work --- .../process/spi/MetadataBuildingProcess.java | 5 +- .../DomainModelCategorizationCollector.java | 9 +-- .../internal/GlobalRegistrationsImpl.java | 6 +- .../categorize/spi/JpaEventListener.java | 56 +++++++++++++++++-- .../spi/ManagedResourcesProcessor.java | 5 +- .../models/xml/XmlProcessingSmokeTests.java | 5 +- 6 files changed, 66 insertions(+), 20 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/process/spi/MetadataBuildingProcess.java b/hibernate-core/src/main/java/org/hibernate/boot/model/process/spi/MetadataBuildingProcess.java index 09f8009697..b41dc4fb0e 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/process/spi/MetadataBuildingProcess.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/process/spi/MetadataBuildingProcess.java @@ -437,10 +437,9 @@ public class MetadataBuildingProcess { final AnnotationDescriptorRegistry descriptorRegistry = sourceModelBuildingContext.getAnnotationDescriptorRegistry(); final DomainModelCategorizationCollector modelCategorizationCollector = new DomainModelCategorizationCollector( areIdGeneratorsGlobal, - classDetailsRegistry, - descriptorRegistry, metadataCollector.getGlobalRegistrations(), - jandexIndex + jandexIndex, + sourceModelBuildingContext ); final XmlProcessingResult xmlProcessingResult = XmlProcessor.processXml( xmlPreProcessingResult, modelCategorizationCollector, sourceModelBuildingContext ); diff --git a/hibernate-core/src/main/java/org/hibernate/boot/models/categorize/internal/DomainModelCategorizationCollector.java b/hibernate-core/src/main/java/org/hibernate/boot/models/categorize/internal/DomainModelCategorizationCollector.java index 9ccd139638..19858bd375 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/models/categorize/internal/DomainModelCategorizationCollector.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/models/categorize/internal/DomainModelCategorizationCollector.java @@ -46,6 +46,7 @@ public class DomainModelCategorizationCollector { private final IndexView jandexIndex; private final GlobalRegistrationsImpl globalRegistrations; + private final SourceModelBuildingContext modelsContext; private final Set rootEntities = new HashSet<>(); private final Map mappedSuperclasses = new HashMap<>(); @@ -53,13 +54,13 @@ public class DomainModelCategorizationCollector { public DomainModelCategorizationCollector( boolean areIdGeneratorsGlobal, - ClassDetailsRegistry classDetailsRegistry, - AnnotationDescriptorRegistry descriptorRegistry, GlobalRegistrations globalRegistrations, - IndexView jandexIndex) { + IndexView jandexIndex, + SourceModelBuildingContext modelsContext) { this.areIdGeneratorsGlobal = areIdGeneratorsGlobal; this.jandexIndex = jandexIndex; this.globalRegistrations = (GlobalRegistrationsImpl) globalRegistrations; + this.modelsContext = modelsContext; } public GlobalRegistrationsImpl getGlobalRegistrations() { @@ -95,7 +96,7 @@ public class DomainModelCategorizationCollector { if ( persistenceUnitDefaults != null ) { final JaxbEntityListenerContainerImpl listenerContainer = persistenceUnitDefaults.getEntityListenerContainer(); if ( listenerContainer != null ) { - getGlobalRegistrations().collectEntityListenerRegistrations( listenerContainer.getEntityListeners() ); + getGlobalRegistrations().collectEntityListenerRegistrations( listenerContainer.getEntityListeners(), modelsContext ); } } } diff --git a/hibernate-core/src/main/java/org/hibernate/boot/models/categorize/internal/GlobalRegistrationsImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/models/categorize/internal/GlobalRegistrationsImpl.java index 1315db731f..6f5d121549 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/models/categorize/internal/GlobalRegistrationsImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/models/categorize/internal/GlobalRegistrationsImpl.java @@ -80,6 +80,7 @@ import org.hibernate.models.spi.AnnotationUsage; import org.hibernate.models.spi.ClassDetails; import org.hibernate.models.spi.ClassDetailsRegistry; import org.hibernate.models.spi.MutableAnnotationUsage; +import org.hibernate.models.spi.SourceModelBuildingContext; import org.hibernate.models.spi.SourceModelContext; import jakarta.persistence.ColumnResult; @@ -583,7 +584,7 @@ public class GlobalRegistrationsImpl implements GlobalRegistrations { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // EntityListenerRegistration - public void collectEntityListenerRegistrations(List listeners) { + public void collectEntityListenerRegistrations(List listeners, SourceModelBuildingContext modelsContext) { if ( CollectionHelper.isEmpty( listeners ) ) { return; } @@ -593,7 +594,8 @@ public class GlobalRegistrationsImpl implements GlobalRegistrations { final JpaEventListener listener = JpaEventListener.from( JpaEventListenerStyle.LISTENER, classDetails, - jaxbEntityListener + jaxbEntityListener, + modelsContext ); addJpaEventListener( listener ); } ); diff --git a/hibernate-core/src/main/java/org/hibernate/boot/models/categorize/spi/JpaEventListener.java b/hibernate-core/src/main/java/org/hibernate/boot/models/categorize/spi/JpaEventListener.java index cbae9bcf08..645d4a07bb 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/models/categorize/spi/JpaEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/models/categorize/spi/JpaEventListener.java @@ -8,10 +8,13 @@ package org.hibernate.boot.models.categorize.spi; import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListenerImpl; import org.hibernate.boot.jaxb.mapping.spi.JaxbPersistenceUnitDefaultsImpl; +import org.hibernate.boot.models.JpaAnnotations; import org.hibernate.internal.util.MutableObject; import org.hibernate.models.ModelsException; import org.hibernate.models.spi.ClassDetails; import org.hibernate.models.spi.MethodDetails; +import org.hibernate.models.spi.MutableMemberDetails; +import org.hibernate.models.spi.SourceModelBuildingContext; import jakarta.persistence.PostLoad; import jakarta.persistence.PostPersist; @@ -22,10 +25,10 @@ import jakarta.persistence.PreRemove; import jakarta.persistence.PreUpdate; /** - * JPA-style event listener with support for resolving callback methods - * {@linkplain #from(JpaEventListenerStyle, ClassDetails, JaxbEntityListenerImpl) by name} (from XML) - * or by {@linkplain #from(JpaEventListenerStyle, ClassDetails) annotation}. - * + * JPA-style event listener with support for resolving callback methods from + * {@linkplain #from(JpaEventListenerStyle, ClassDetails, JaxbEntityListenerImpl, SourceModelBuildingContext) XML} + * or from {@linkplain #from(JpaEventListenerStyle, ClassDetails) annotation}. + *

* Represents a global entity listener defined in the persistence unit * * @see JaxbPersistenceUnitDefaultsImpl#getEntityListenerContainer() @@ -117,7 +120,8 @@ public class JpaEventListener { public static JpaEventListener from( JpaEventListenerStyle consumerType, ClassDetails listenerClassDetails, - JaxbEntityListenerImpl jaxbMapping) { + JaxbEntityListenerImpl jaxbMapping, + SourceModelBuildingContext modelsContext) { final MutableObject prePersistMethod = new MutableObject<>(); final MutableObject postPersistMethod = new MutableObject<>(); final MutableObject preRemoveMethod = new MutableObject<>(); @@ -135,36 +139,78 @@ public class JpaEventListener { && methodDetails.getName().equals( jaxbMapping.getPrePersist().getMethodName() ) && matchesSignature( consumerType, methodDetails ) ) { prePersistMethod.set( methodDetails ); + ( (MutableMemberDetails) methodDetails ).addAnnotationUsage( + JpaAnnotations.PRE_PERSIST.createUsage( + methodDetails, + modelsContext + ) + ); } else if ( jaxbMapping.getPostPersist() != null && methodDetails.getName().equals( jaxbMapping.getPostPersist().getMethodName() ) && matchesSignature( consumerType, methodDetails ) ) { postPersistMethod.set( methodDetails ); + ( (MutableMemberDetails) methodDetails ).addAnnotationUsage( + JpaAnnotations.POST_PERSIST.createUsage( + methodDetails, + modelsContext + ) + ); } else if ( jaxbMapping.getPreRemove() != null && methodDetails.getName().equals( jaxbMapping.getPreRemove().getMethodName() ) && matchesSignature( consumerType, methodDetails ) ) { preRemoveMethod.set( methodDetails ); + ( (MutableMemberDetails) methodDetails ).addAnnotationUsage( + JpaAnnotations.PRE_REMOVE.createUsage( + methodDetails, + modelsContext + ) + ); } else if ( jaxbMapping.getPostRemove() != null && methodDetails.getName().equals( jaxbMapping.getPostRemove().getMethodName() ) && matchesSignature( consumerType, methodDetails ) ) { postRemoveMethod.set( methodDetails ); + ( (MutableMemberDetails) methodDetails ).addAnnotationUsage( + JpaAnnotations.POST_REMOVE.createUsage( + methodDetails, + modelsContext + ) + ); } else if ( jaxbMapping.getPreUpdate() != null && methodDetails.getName().equals( jaxbMapping.getPreUpdate().getMethodName() ) && matchesSignature( consumerType, methodDetails ) ) { preUpdateMethod.set( methodDetails ); + ( (MutableMemberDetails) methodDetails ).addAnnotationUsage( + JpaAnnotations.PRE_UPDATE.createUsage( + methodDetails, + modelsContext + ) + ); } else if ( jaxbMapping.getPostUpdate() != null && methodDetails.getName().equals( jaxbMapping.getPostUpdate().getMethodName() ) && matchesSignature( consumerType, methodDetails ) ) { postUpdateMethod.set( methodDetails ); + ( (MutableMemberDetails) methodDetails ).addAnnotationUsage( + JpaAnnotations.POST_UPDATE.createUsage( + methodDetails, + modelsContext + ) + ); } else if ( jaxbMapping.getPostLoad() != null && methodDetails.getName().equals( jaxbMapping.getPostLoad().getMethodName() ) && matchesSignature( consumerType, methodDetails ) ) { postLoadMethod.set( methodDetails ); + ( (MutableMemberDetails) methodDetails ).addAnnotationUsage( + JpaAnnotations.POST_LOAD.createUsage( + methodDetails, + modelsContext + ) + ); } } ); diff --git a/hibernate-core/src/main/java/org/hibernate/boot/models/categorize/spi/ManagedResourcesProcessor.java b/hibernate-core/src/main/java/org/hibernate/boot/models/categorize/spi/ManagedResourcesProcessor.java index 308e46cf01..bda03b4ebc 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/models/categorize/spi/ManagedResourcesProcessor.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/models/categorize/spi/ManagedResourcesProcessor.java @@ -128,10 +128,9 @@ public class ManagedResourcesProcessor { final GlobalRegistrationsImpl globalRegistrations = new GlobalRegistrationsImpl( sourceModelBuildingContext ); final DomainModelCategorizationCollector modelCategorizationCollector = new DomainModelCategorizationCollector( areIdGeneratorsGlobal, - classDetailsRegistry, - descriptorRegistry, globalRegistrations, - jandexIndex + jandexIndex, + sourceModelBuildingContext ); final XmlProcessingResult xmlProcessingResult = XmlProcessor.processXml( xmlPreProcessingResult, modelCategorizationCollector, sourceModelBuildingContext ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/boot/models/xml/XmlProcessingSmokeTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/boot/models/xml/XmlProcessingSmokeTests.java index b323e5af47..e7a1633e17 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/boot/models/xml/XmlProcessingSmokeTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/boot/models/xml/XmlProcessingSmokeTests.java @@ -126,10 +126,9 @@ public class XmlProcessingSmokeTests { final DomainModelCategorizationCollector collector = new DomainModelCategorizationCollector( false, - buildingContext.getClassDetailsRegistry(), - buildingContext.getAnnotationDescriptorRegistry(), new GlobalRegistrationsImpl( buildingContext ), - null + null, + buildingContext ); collectedXmlResources.getDocuments().forEach( collector::apply );