Skip to content

Commit a2addf4

Browse files
committed
HHH-19247 Add ordinal() also to Type contributions to be able to influence the order in which serviceloaded custom types are registered
1 parent cc05e22 commit a2addf4

File tree

4 files changed

+63
-76
lines changed

4 files changed

+63
-76
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/FunctionContributor.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,34 @@
3131
* or even {@link org.hibernate.boot.MetadataBuilder#applyFunctions(FunctionContributor)}.
3232
* </ul>
3333
*
34-
* @author Karel Maesen
3534
* @see org.hibernate.query.sqm.function.SqmFunctionRegistry
35+
*
36+
* @author Karel Maesen
3637
*/
3738
@JavaServiceLoadable
38-
public interface FunctionContributor extends Ordinated {
39+
public interface FunctionContributor {
3940

4041
/**
4142
* Contribute functions
4243
*
4344
* @param functionContributions The target for the contributions
4445
*/
4546
void contributeFunctions(FunctionContributions functionContributions);
47+
48+
/**
49+
* Determines order in which the contributions will be applied
50+
* (lowest ordinal first).
51+
* <p>
52+
* The range 0-500 is reserved for Hibernate, range 500-1000 for libraries and
53+
* 1000-Integer.MAX_VALUE for user-defined FunctionContributors.
54+
* <p>
55+
* Contributions from higher precedence contributors (higher numbers) effectively override
56+
* contributions from lower precedence. E.g. if a contributor with precedence 1000 contributes a
57+
* function named {@code "max"}, that will override Hibernate's standard function of that name.
58+
*
59+
* @return the ordinal for this FunctionContributor
60+
*/
61+
default int ordinal(){
62+
return 1000;
63+
}
4664
}

hibernate-core/src/main/java/org/hibernate/boot/model/Ordinated.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

hibernate-core/src/main/java/org/hibernate/boot/model/TypeContributor.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,39 @@
2323
* or even {@link org.hibernate.boot.MetadataBuilder#applyTypes(TypeContributor)}.
2424
* <li>
2525
* When bootstrapping Hibernate via JPA or {@link org.hibernate.cfg.Configuration},
26-
* <p>
26+
*
2727
* Finally, in the JPA boostrap process, {@code TypeContributor}s may be
2828
* listed via {@link org.hibernate.jpa.boot.spi.JpaSettings#TYPE_CONTRIBUTORS}.
2929
* </ul>
3030
*
3131
* @author Steve Ebersole
32+
*
3233
* @see org.hibernate.type.spi.TypeConfiguration
3334
*/
3435
@JavaServiceLoadable
35-
public interface TypeContributor extends Ordinated {
36+
public interface TypeContributor {
3637
/**
3738
* Contribute types
3839
*
3940
* @param typeContributions The callback for adding contributed types
4041
* @param serviceRegistry The service registry
4142
*/
4243
void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry);
44+
45+
/**
46+
* Determines order in which the contributions will be applied
47+
* (lowest ordinal first).
48+
* <p>
49+
* The range 0-500 is reserved for Hibernate, range 500-1000 for libraries and
50+
* 1000-Integer.MAX_VALUE for user-defined TypeContributors.
51+
* <p>
52+
* Contributions from higher precedence contributors (higher numbers) effectively override
53+
* contributions from lower precedence. E.g. if a contributor with precedence 2000 contributes
54+
* some type, that will override Hibernate's standard type of that name.
55+
*
56+
* @return the ordinal for this TypenContributor
57+
*/
58+
default int ordinal(){
59+
return 1000;
60+
}
4361
}

hibernate-core/src/main/java/org/hibernate/boot/model/process/spi/MetadataBuildingProcess.java

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,18 @@
9696
/**
9797
* Represents the process of transforming a {@link MetadataSources}
9898
* reference into a {@link org.hibernate.boot.Metadata} reference. Allows for 2 different process paradigms:<ul>
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>
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>
111111
* </ul>
112112
*
113113
* @author Steve Ebersole
@@ -200,7 +200,7 @@ public static MetadataImplementor complete(
200200

201201
final MetadataSourceProcessor processor = new MetadataSourceProcessor() {
202202
private final MetadataSourceProcessor hbmProcessor =
203-
options.isXmlMappingEnabled()
203+
options.isXmlMappingEnabled()
204204
? new HbmMetadataSourceProcessorImpl( managedResources, rootMetadataBuildingContext )
205205
: new NoOpMetadataSourceProcessorImpl();
206206

@@ -339,19 +339,8 @@ public void finishUp() {
339339

340340
processor.finishUp();
341341

342-
processAdditionalMappingContributions(
343-
metadataCollector,
344-
options,
345-
classLoaderService,
346-
rootMetadataBuildingContext
347-
);
348-
processAdditionalJaxbMappingProducer(
349-
metadataCollector,
350-
options,
351-
jandexView,
352-
classLoaderService,
353-
rootMetadataBuildingContext
354-
);
342+
processAdditionalMappingContributions( metadataCollector, options, classLoaderService, rootMetadataBuildingContext );
343+
processAdditionalJaxbMappingProducer( metadataCollector, options, jandexView, classLoaderService, rootMetadataBuildingContext );
355344

356345
applyExtraQueryImports( managedResources, metadataCollector );
357346

@@ -391,8 +380,7 @@ public boolean transformHbmMappings() {
391380
rootMetadataBuildingContext
392381
);
393382

394-
final Collection<AdditionalMappingContributor> additionalMappingContributors = classLoaderService.loadJavaServices(
395-
AdditionalMappingContributor.class );
383+
final Collection<AdditionalMappingContributor> additionalMappingContributors = classLoaderService.loadJavaServices( AdditionalMappingContributor.class );
396384
additionalMappingContributors.forEach( (contributor) -> {
397385
contributions.setCurrentContributor( contributor.getContributorName() );
398386
try {
@@ -463,7 +451,7 @@ public void contributeBinding(InputStream xmlStream) {
463451

464452
@Override
465453
public void contributeBinding(JaxbEntityMappings mappingJaxbBinding) {
466-
if ( !options.isXmlMappingEnabled() ) {
454+
if ( ! options.isXmlMappingEnabled() ) {
467455
return;
468456
}
469457

@@ -475,7 +463,7 @@ public void contributeBinding(JaxbEntityMappings mappingJaxbBinding) {
475463

476464
@Override
477465
public void contributeBinding(JaxbHbmHibernateMapping hbmJaxbBinding) {
478-
if ( !options.isXmlMappingEnabled() ) {
466+
if ( ! options.isXmlMappingEnabled() ) {
479467
return;
480468
}
481469

@@ -540,8 +528,7 @@ private static void processAdditionalJaxbMappingProducer(
540528
ClassLoaderService classLoaderService,
541529
MetadataBuildingContextRootImpl rootMetadataBuildingContext) {
542530
if ( options.isXmlMappingEnabled() ) {
543-
final Iterable<AdditionalJaxbMappingProducer> producers = classLoaderService.loadJavaServices(
544-
AdditionalJaxbMappingProducer.class );
531+
final Iterable<AdditionalJaxbMappingProducer> producers = classLoaderService.loadJavaServices( AdditionalJaxbMappingProducer.class );
545532
if ( producers != null ) {
546533
final EntityHierarchyBuilder hierarchyBuilder = new EntityHierarchyBuilder();
547534
final MappingBinder mappingBinder = new MappingBinder(
@@ -611,7 +598,7 @@ private static void handleTypes(
611598
MetadataBuildingOptions options,
612599
InFlightMetadataCollector metadataCollector) {
613600
final ClassLoaderService classLoaderService =
614-
options.getServiceRegistry().requireService( ClassLoaderService.class );
601+
options.getServiceRegistry().requireService(ClassLoaderService.class);
615602

616603
final TypeConfiguration typeConfiguration = bootstrapContext.getTypeConfiguration();
617604
final StandardServiceRegistry serviceRegistry = bootstrapContext.getServiceRegistry();
@@ -645,7 +632,7 @@ public void contributeType(CompositeUserType<?> type) {
645632
basicTypeRegistry.addTypeReferenceRegistrationKey(
646633
StandardBasicTypes.BINARY_WRAPPER.getName(),
647634
Byte[].class.getName(), "Byte[]"
648-
);
635+
);
649636
}
650637

651638
// add Dialect contributed types
@@ -855,9 +842,7 @@ private static void adaptTimestampTypesToDefaultTimeZoneStorage(
855842
);
856843
}
857844

858-
private static JdbcType getTimeWithTimeZoneOverride(
859-
MetadataBuildingOptions options,
860-
JdbcTypeRegistry jdbcTypeRegistry) {
845+
private static JdbcType getTimeWithTimeZoneOverride(MetadataBuildingOptions options, JdbcTypeRegistry jdbcTypeRegistry) {
861846
switch ( options.getDefaultTimeZoneStorage() ) {
862847
case NORMALIZE:
863848
// For NORMALIZE, we replace the standard types that use TIME_WITH_TIMEZONE to use TIME
@@ -870,9 +855,7 @@ private static JdbcType getTimeWithTimeZoneOverride(
870855
}
871856
}
872857

873-
private static JdbcType getTimestampWithTimeZoneOverride(
874-
MetadataBuildingOptions options,
875-
JdbcTypeRegistry jdbcTypeRegistry) {
858+
private static JdbcType getTimestampWithTimeZoneOverride(MetadataBuildingOptions options, JdbcTypeRegistry jdbcTypeRegistry) {
876859
switch ( options.getDefaultTimeZoneStorage() ) {
877860
case NORMALIZE:
878861
// For NORMALIZE, we replace the standard types that use TIMESTAMP_WITH_TIMEZONE to use TIMESTAMP

0 commit comments

Comments
 (0)