4242import org .hibernate .id .uuid .UuidValueGenerator ;
4343import org .hibernate .internal .CoreLogging ;
4444import org .hibernate .internal .CoreMessageLogger ;
45- import org .hibernate .internal .util .StringHelper ;
4645import org .hibernate .mapping .GeneratorCreator ;
4746import org .hibernate .mapping .KeyValue ;
4847import org .hibernate .mapping .PersistentClass ;
7069import static org .hibernate .boot .model .internal .GeneratorStrategies .generatorClass ;
7170import static org .hibernate .id .IdentifierGenerator .GENERATOR_NAME ;
7271import static org .hibernate .internal .util .NullnessUtil .castNonNull ;
72+ import static org .hibernate .internal .util .StringHelper .isNotEmpty ;
7373import static org .hibernate .internal .util .StringHelper .qualify ;
7474import static org .hibernate .internal .util .collections .CollectionHelper .combineUntyped ;
75- import static org .hibernate .resource .beans .internal .Helper .allowExtensionsInCdi ;
7675
7776/**
7877 * Responsible for configuring and instantiating {@link Generator}s.
@@ -120,32 +119,13 @@ public static void makeIdGenerator(
120119 if ( generatedValue != null ) {
121120 // The mapping used @GeneratedValue but specified no name. This is a special case added in JPA 3.2.
122121 // Look for a matching "implied generator" based on the GenerationType
123-
124122 final GenerationType strategy = generatedValue .strategy ();
125123 final String strategyGeneratorClassName = correspondingGeneratorName ( strategy );
126-
127- final IdentifierGeneratorDefinition impliedGenerator = determineImpliedGenerator (
128- strategy ,
129- strategyGeneratorClassName ,
130- localGenerators
131- );
132-
124+ final IdentifierGeneratorDefinition impliedGenerator =
125+ determineImpliedGenerator ( strategy , strategyGeneratorClassName , localGenerators );
133126 if ( impliedGenerator != null ) {
134127 configuration .putAll ( impliedGenerator .getParameters () );
135-
136- final BeanContainer beanContainer = beanContainer ( context );
137- identifierValue .setCustomIdGeneratorCreator ( creationContext -> {
138- final Generator identifierGenerator = instantiateGenerator (
139- beanContainer ,
140- generatorClass ( strategyGeneratorClassName , identifierValue )
141- );
142- callConfigure ( creationContext , identifierGenerator , configuration , identifierValue );
143- if ( identifierGenerator instanceof IdentityGenerator ) {
144- identifierValue .setColumnToIdentity ();
145- }
146- return identifierGenerator ;
147- } );
148-
128+ instantiateGeneratorBean ( identifierValue , strategyGeneratorClassName , configuration , context );
149129 return ;
150130 }
151131 }
@@ -171,7 +151,8 @@ private static IdentifierGeneratorDefinition determineImpliedGenerator(
171151 }
172152
173153 if ( localGenerators .size () == 1 ) {
174- final IdentifierGeneratorDefinition generatorDefinition = localGenerators .entrySet ().iterator ().next ().getValue ();
154+ final IdentifierGeneratorDefinition generatorDefinition =
155+ localGenerators .values ().iterator ().next ();
175156 // NOTE : a little bit of a special rule here for the case of just one -
176157 // we consider it a match, based on strategy, if the strategy is AUTO or matches...
177158 if ( strategy == AUTO
@@ -181,13 +162,13 @@ private static IdentifierGeneratorDefinition determineImpliedGenerator(
181162 }
182163
183164 IdentifierGeneratorDefinition matching = null ;
184- for ( Map . Entry < String , ? extends IdentifierGeneratorDefinition > localGeneratorEntry : localGenerators .entrySet () ) {
185- if ( isImpliedGenerator ( strategy , strategyGeneratorClassName , localGeneratorEntry . getValue () ) ) {
165+ for ( IdentifierGeneratorDefinition localGenerator : localGenerators .values () ) {
166+ if ( isImpliedGenerator ( strategy , strategyGeneratorClassName , localGenerator ) ) {
186167 if ( matching != null ) {
187168 // we found multiple matching generators
188169 return null ;
189170 }
190- matching = localGeneratorEntry . getValue () ;
171+ matching = localGenerator ;
191172 }
192173 }
193174 return matching ;
@@ -259,20 +240,20 @@ private static IdentifierGeneratorDefinition makeIdentifierGeneratorDefinition(
259240 if ( globalDefinition != null ) {
260241 return globalDefinition ;
261242 }
262-
263- LOG .debugf ( "Could not resolve explicit IdentifierGeneratorDefinition - using implicit interpretation (%s)" , name );
264-
265- final GeneratedValue generatedValue = idAttributeMember .getDirectAnnotationUsage ( GeneratedValue .class );
266- if ( generatedValue == null ) {
267- throw new AssertionFailure ( "No @GeneratedValue annotation" );
243+ else {
244+ LOG .debugf ( "Could not resolve explicit IdentifierGeneratorDefinition - using implicit interpretation (%s)" ,
245+ name );
246+ final GeneratedValue generatedValue = idAttributeMember .getDirectAnnotationUsage ( GeneratedValue .class );
247+ if ( generatedValue == null ) {
248+ throw new AssertionFailure ( "No @GeneratedValue annotation" );
249+ }
250+ return IdentifierGeneratorDefinition .createImplicit (
251+ name ,
252+ idAttributeMember .getType (),
253+ generatedValue .generator (),
254+ interpretGenerationType ( generatedValue )
255+ );
268256 }
269-
270- return IdentifierGeneratorDefinition .createImplicit (
271- name ,
272- idAttributeMember .getType (),
273- generatedValue .generator (),
274- interpretGenerationType ( generatedValue )
275- );
276257 }
277258
278259 private static GenerationType interpretGenerationType (GeneratedValue generatedValueAnn ) {
@@ -285,43 +266,35 @@ public static void visitIdGeneratorDefinitions(
285266 AnnotationTarget annotatedElement ,
286267 Consumer <IdentifierGeneratorDefinition > consumer ,
287268 MetadataBuildingContext context ) {
288- final InFlightMetadataCollector metadataCollector = context . getMetadataCollector ();
289- final SourceModelBuildingContext sourceModelContext = metadataCollector .getSourceModelBuildingContext ();
269+ final SourceModelBuildingContext sourceModelContext =
270+ context . getMetadataCollector () .getSourceModelBuildingContext ();
290271
291- annotatedElement .forEachAnnotationUsage ( TableGenerator .class , sourceModelContext , (usage ) -> {
292- final IdentifierGeneratorDefinition idGenerator = buildTableIdGenerator ( usage );
293- consumer .accept ( idGenerator );
294- } );
272+ annotatedElement .forEachAnnotationUsage ( TableGenerator .class , sourceModelContext ,
273+ usage -> consumer .accept ( buildTableIdGenerator ( usage ) ) );
295274
296- annotatedElement .forEachAnnotationUsage ( SequenceGenerator .class , sourceModelContext , (usage ) -> {
297- final IdentifierGeneratorDefinition idGenerator = buildSequenceIdGenerator ( usage );
298- consumer .accept ( idGenerator );
299- } );
275+ annotatedElement .forEachAnnotationUsage ( SequenceGenerator .class , sourceModelContext ,
276+ usage -> consumer .accept ( buildSequenceIdGenerator ( usage ) ) );
300277
301- annotatedElement .forEachAnnotationUsage ( GenericGenerator .class , sourceModelContext , (usage ) -> {
302- final IdentifierGeneratorDefinition idGenerator = buildIdGenerator ( usage );
303- consumer .accept ( idGenerator );
304- } );
278+ annotatedElement .forEachAnnotationUsage ( GenericGenerator .class , sourceModelContext ,
279+ usage -> consumer .accept ( buildIdGenerator ( usage ) ) );
305280
306281 }
307282
308283 public static void registerGlobalGenerators (
309284 AnnotationTarget annotatedElement ,
310285 MetadataBuildingContext context ) {
311- if ( !context .getBootstrapContext ().getJpaCompliance ().isGlobalGeneratorScopeEnabled () ) {
312- return ;
286+ if ( context .getBootstrapContext ().getJpaCompliance ().isGlobalGeneratorScopeEnabled () ) {
287+ final InFlightMetadataCollector metadataCollector = context .getMetadataCollector ();
288+ visitIdGeneratorDefinitions (
289+ annotatedElement ,
290+ definition -> {
291+ if ( !definition .getName ().isEmpty () ) {
292+ metadataCollector .addIdentifierGenerator ( definition );
293+ }
294+ },
295+ context
296+ );
313297 }
314-
315- final InFlightMetadataCollector metadataCollector = context .getMetadataCollector ();
316- visitIdGeneratorDefinitions (
317- annotatedElement ,
318- (definition ) -> {
319- if ( !definition .getName ().isEmpty () ) {
320- metadataCollector .addIdentifierGenerator ( definition );
321- }
322- },
323- context
324- );
325298 }
326299
327300 private static IdentifierGeneratorDefinition buildIdGenerator (GenericGenerator generatorAnnotation ) {
@@ -580,12 +553,9 @@ private static <G extends Generator> G instantiateGenerator(
580553 public static <T extends Generator > T instantiateGenerator (
581554 BeanContainer beanContainer ,
582555 Class <T > generatorClass ) {
583- if ( beanContainer != null ) {
584- return instantiateGeneratorAsBean ( beanContainer , generatorClass );
585- }
586- else {
587- return instantiateGeneratorViaDefaultConstructor ( generatorClass );
588- }
556+ return beanContainer != null
557+ ? instantiateGeneratorAsBean ( beanContainer , generatorClass )
558+ : instantiateGeneratorViaDefaultConstructor ( generatorClass );
589559 }
590560
591561 /**
@@ -712,19 +682,7 @@ public static void createGeneratorFrom(
712682 Map <String , Object > configuration ,
713683 MetadataBuildingContext context ) {
714684 configuration .putAll ( defaultedGenerator .getParameters () );
715-
716- final BeanContainer beanContainer = beanContainer ( context );
717- idValue .setCustomIdGeneratorCreator ( creationContext -> {
718- final Generator identifierGenerator = instantiateGenerator (
719- beanContainer ,
720- generatorClass ( defaultedGenerator .getStrategy (), idValue )
721- );
722- callConfigure ( creationContext , identifierGenerator , configuration , idValue );
723- if ( identifierGenerator instanceof IdentityGenerator ) {
724- idValue .setColumnToIdentity ();
725- }
726- return identifierGenerator ;
727- } );
685+ instantiateGeneratorBean ( idValue , defaultedGenerator .getStrategy (), configuration , context );
728686 }
729687
730688
@@ -802,19 +760,27 @@ private static void setGeneratorCreator(
802760 identifierValue .setCustomIdGeneratorCreator ( ASSIGNED_IDENTIFIER_GENERATOR_CREATOR );
803761 }
804762 else {
805- final BeanContainer beanContainer = beanContainer ( context );
806- identifierValue .setCustomIdGeneratorCreator ( creationContext -> {
807- final Generator identifierGenerator =
808- instantiateGenerator ( beanContainer , generatorClass ( generatorStrategy , identifierValue ) );
809- callConfigure ( creationContext , identifierGenerator , configuration , identifierValue );
810- if ( identifierGenerator instanceof IdentityGenerator ) {
811- identifierValue .setColumnToIdentity ();
812- }
813- return identifierGenerator ;
814- } );
763+ instantiateGeneratorBean ( identifierValue , generatorStrategy , configuration , context );
815764 }
816765 }
817766
767+ private static void instantiateGeneratorBean (
768+ SimpleValue identifierValue ,
769+ String generatorStrategy ,
770+ Map <String , Object > configuration ,
771+ MetadataBuildingContext context ) {
772+ final BeanContainer beanContainer = beanContainer ( context );
773+ identifierValue .setCustomIdGeneratorCreator ( creationContext -> {
774+ final Generator identifierGenerator =
775+ instantiateGenerator ( beanContainer , generatorClass ( generatorStrategy , identifierValue ) );
776+ callConfigure ( creationContext , identifierGenerator , configuration , identifierValue );
777+ if ( identifierGenerator instanceof IdentityGenerator ) {
778+ identifierValue .setColumnToIdentity ();
779+ }
780+ return identifierGenerator ;
781+ } );
782+ }
783+
818784 /**
819785 * Set up the id generator by considering all annotations of the identifier
820786 * field, including {@linkplain IdGeneratorType id generator annotations},
@@ -890,7 +856,7 @@ static GeneratorCreator createValueGeneratorFromAnnotations(
890856 }
891857
892858 public static void applyIfNotEmpty (String name , String value , BiConsumer <String ,String > consumer ) {
893- if ( StringHelper . isNotEmpty ( value ) ) {
859+ if ( isNotEmpty ( value ) ) {
894860 consumer .accept ( name , value );
895861 }
896862 }
0 commit comments