8787
8888import jakarta .persistence .AttributeConverter ;
8989
90+ import static java .util .Comparator .comparingInt ;
9091import static org .hibernate .internal .util .config .ConfigurationHelper .getPreferredSqlTypeCodeForArray ;
9192import static org .hibernate .internal .util .config .ConfigurationHelper .getPreferredSqlTypeCodeForDuration ;
9293import static org .hibernate .internal .util .config .ConfigurationHelper .getPreferredSqlTypeCodeForInstant ;
9596/**
9697 * Represents the process of transforming a {@link MetadataSources}
9798 * reference into a {@link org.hibernate.boot.Metadata} reference. Allows for 2 different process paradigms:<ul>
98- * <li>
99- * Single step : as defined by the {@link #build} method; internally leverages the 2-step paradigm
100- * </li>
101- * <li>
102- * Two step : a first step coordinates resource scanning and some other preparation work; a second step
103- * builds the {@link org.hibernate.boot.Metadata}. A hugely important distinction in the need for the
104- * steps is that the first phase should strive to not load user entity/component classes so that we can still
105- * perform enhancement on them later. This approach caters to the 2-phase bootstrap we use in regards
106- * to WildFly Hibernate-JPA integration. The first step is defined by {@link #prepare} which returns
107- * a {@link ManagedResources} instance. The second step is defined by calling
108- * {@link #complete}
109- * </li>
99+ * <li>
100+ * Single step : as defined by the {@link #build} method; internally leverages the 2-step paradigm
101+ * </li>
102+ * <li>
103+ * Two step : a first step coordinates resource scanning and some other preparation work; a second step
104+ * builds the {@link org.hibernate.boot.Metadata}. A hugely important distinction in the need for the
105+ * steps is that the first phase should strive to not load user entity/component classes so that we can still
106+ * perform enhancement on them later. This approach caters to the 2-phase bootstrap we use in regards
107+ * to WildFly Hibernate-JPA integration. The first step is defined by {@link #prepare} which returns
108+ * a {@link ManagedResources} instance. The second step is defined by calling
109+ * {@link #complete}
110+ * </li>
110111 * </ul>
111112 *
112113 * @author Steve Ebersole
@@ -199,7 +200,7 @@ public static MetadataImplementor complete(
199200
200201 final MetadataSourceProcessor processor = new MetadataSourceProcessor () {
201202 private final MetadataSourceProcessor hbmProcessor =
202- options .isXmlMappingEnabled ()
203+ options .isXmlMappingEnabled ()
203204 ? new HbmMetadataSourceProcessorImpl ( managedResources , rootMetadataBuildingContext )
204205 : new NoOpMetadataSourceProcessorImpl ();
205206
@@ -338,8 +339,19 @@ public void finishUp() {
338339
339340 processor .finishUp ();
340341
341- processAdditionalMappingContributions ( metadataCollector , options , classLoaderService , rootMetadataBuildingContext );
342- processAdditionalJaxbMappingProducer ( metadataCollector , options , jandexView , classLoaderService , rootMetadataBuildingContext );
342+ processAdditionalMappingContributions (
343+ metadataCollector ,
344+ options ,
345+ classLoaderService ,
346+ rootMetadataBuildingContext
347+ );
348+ processAdditionalJaxbMappingProducer (
349+ metadataCollector ,
350+ options ,
351+ jandexView ,
352+ classLoaderService ,
353+ rootMetadataBuildingContext
354+ );
343355
344356 applyExtraQueryImports ( managedResources , metadataCollector );
345357
@@ -379,7 +391,8 @@ public boolean transformHbmMappings() {
379391 rootMetadataBuildingContext
380392 );
381393
382- final Collection <AdditionalMappingContributor > additionalMappingContributors = classLoaderService .loadJavaServices ( AdditionalMappingContributor .class );
394+ final Collection <AdditionalMappingContributor > additionalMappingContributors = classLoaderService .loadJavaServices (
395+ AdditionalMappingContributor .class );
383396 additionalMappingContributors .forEach ( (contributor ) -> {
384397 contributions .setCurrentContributor ( contributor .getContributorName () );
385398 try {
@@ -450,7 +463,7 @@ public void contributeBinding(InputStream xmlStream) {
450463
451464 @ Override
452465 public void contributeBinding (JaxbEntityMappings mappingJaxbBinding ) {
453- if ( ! options .isXmlMappingEnabled () ) {
466+ if ( !options .isXmlMappingEnabled () ) {
454467 return ;
455468 }
456469
@@ -462,7 +475,7 @@ public void contributeBinding(JaxbEntityMappings mappingJaxbBinding) {
462475
463476 @ Override
464477 public void contributeBinding (JaxbHbmHibernateMapping hbmJaxbBinding ) {
465- if ( ! options .isXmlMappingEnabled () ) {
478+ if ( !options .isXmlMappingEnabled () ) {
466479 return ;
467480 }
468481
@@ -527,7 +540,8 @@ private static void processAdditionalJaxbMappingProducer(
527540 ClassLoaderService classLoaderService ,
528541 MetadataBuildingContextRootImpl rootMetadataBuildingContext ) {
529542 if ( options .isXmlMappingEnabled () ) {
530- final Iterable <AdditionalJaxbMappingProducer > producers = classLoaderService .loadJavaServices ( AdditionalJaxbMappingProducer .class );
543+ final Iterable <AdditionalJaxbMappingProducer > producers = classLoaderService .loadJavaServices (
544+ AdditionalJaxbMappingProducer .class );
531545 if ( producers != null ) {
532546 final EntityHierarchyBuilder hierarchyBuilder = new EntityHierarchyBuilder ();
533547 final MappingBinder mappingBinder = new MappingBinder (
@@ -597,7 +611,7 @@ private static void handleTypes(
597611 MetadataBuildingOptions options ,
598612 InFlightMetadataCollector metadataCollector ) {
599613 final ClassLoaderService classLoaderService =
600- options .getServiceRegistry ().requireService (ClassLoaderService .class );
614+ options .getServiceRegistry ().requireService ( ClassLoaderService .class );
601615
602616 final TypeConfiguration typeConfiguration = bootstrapContext .getTypeConfiguration ();
603617 final StandardServiceRegistry serviceRegistry = bootstrapContext .getServiceRegistry ();
@@ -631,7 +645,7 @@ public void contributeType(CompositeUserType<?> type) {
631645 basicTypeRegistry .addTypeReferenceRegistrationKey (
632646 StandardBasicTypes .BINARY_WRAPPER .getName (),
633647 Byte [].class .getName (), "Byte[]"
634- );
648+ );
635649 }
636650
637651 // add Dialect contributed types
@@ -642,7 +656,7 @@ public void contributeType(CompositeUserType<?> type) {
642656 final JdbcType dialectArrayDescriptor = jdbcTypeRegistry .findDescriptor ( SqlTypes .ARRAY );
643657
644658 // add TypeContributor contributed types.
645- for ( TypeContributor contributor : classLoaderService . loadJavaServices ( TypeContributor . class ) ) {
659+ for ( TypeContributor contributor : sortedTypeContributors ( classLoaderService ) ) {
646660 contributor .contribute ( typeContributions , options .getServiceRegistry () );
647661 }
648662
@@ -749,6 +763,17 @@ public void contributeType(CompositeUserType<?> type) {
749763 }
750764 }
751765
766+ private static List <TypeContributor > sortedTypeContributors (
767+ ClassLoaderService classLoaderService ) {
768+ Collection <TypeContributor > typeContributors = classLoaderService .loadJavaServices ( TypeContributor .class );
769+ List <TypeContributor > contributors = new ArrayList <>( typeContributors );
770+ contributors .sort (
771+ comparingInt ( TypeContributor ::ordinal )
772+ .thenComparing ( a -> a .getClass ().getCanonicalName () )
773+ );
774+ return contributors ;
775+ }
776+
752777 private static void adaptToPreferredSqlTypeCode (
753778 JdbcTypeRegistry jdbcTypeRegistry ,
754779 JdbcType dialectUuidDescriptor ,
@@ -830,7 +855,9 @@ private static void adaptTimestampTypesToDefaultTimeZoneStorage(
830855 );
831856 }
832857
833- private static JdbcType getTimeWithTimeZoneOverride (MetadataBuildingOptions options , JdbcTypeRegistry jdbcTypeRegistry ) {
858+ private static JdbcType getTimeWithTimeZoneOverride (
859+ MetadataBuildingOptions options ,
860+ JdbcTypeRegistry jdbcTypeRegistry ) {
834861 switch ( options .getDefaultTimeZoneStorage () ) {
835862 case NORMALIZE :
836863 // For NORMALIZE, we replace the standard types that use TIME_WITH_TIMEZONE to use TIME
@@ -843,7 +870,9 @@ private static JdbcType getTimeWithTimeZoneOverride(MetadataBuildingOptions opti
843870 }
844871 }
845872
846- private static JdbcType getTimestampWithTimeZoneOverride (MetadataBuildingOptions options , JdbcTypeRegistry jdbcTypeRegistry ) {
873+ private static JdbcType getTimestampWithTimeZoneOverride (
874+ MetadataBuildingOptions options ,
875+ JdbcTypeRegistry jdbcTypeRegistry ) {
847876 switch ( options .getDefaultTimeZoneStorage () ) {
848877 case NORMALIZE :
849878 // For NORMALIZE, we replace the standard types that use TIMESTAMP_WITH_TIMEZONE to use TIMESTAMP
0 commit comments