42
42
import org .hibernate .id .uuid .UuidValueGenerator ;
43
43
import org .hibernate .internal .CoreLogging ;
44
44
import org .hibernate .internal .CoreMessageLogger ;
45
- import org .hibernate .internal .util .StringHelper ;
46
45
import org .hibernate .mapping .GeneratorCreator ;
47
46
import org .hibernate .mapping .KeyValue ;
48
47
import org .hibernate .mapping .PersistentClass ;
70
69
import static org .hibernate .boot .model .internal .GeneratorStrategies .generatorClass ;
71
70
import static org .hibernate .id .IdentifierGenerator .GENERATOR_NAME ;
72
71
import static org .hibernate .internal .util .NullnessUtil .castNonNull ;
72
+ import static org .hibernate .internal .util .StringHelper .isNotEmpty ;
73
73
import static org .hibernate .internal .util .StringHelper .qualify ;
74
74
import static org .hibernate .internal .util .collections .CollectionHelper .combineUntyped ;
75
- import static org .hibernate .resource .beans .internal .Helper .allowExtensionsInCdi ;
76
75
77
76
/**
78
77
* Responsible for configuring and instantiating {@link Generator}s.
@@ -120,32 +119,13 @@ public static void makeIdGenerator(
120
119
if ( generatedValue != null ) {
121
120
// The mapping used @GeneratedValue but specified no name. This is a special case added in JPA 3.2.
122
121
// Look for a matching "implied generator" based on the GenerationType
123
-
124
122
final GenerationType strategy = generatedValue .strategy ();
125
123
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 );
133
126
if ( impliedGenerator != null ) {
134
127
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 );
149
129
return ;
150
130
}
151
131
}
@@ -171,7 +151,8 @@ private static IdentifierGeneratorDefinition determineImpliedGenerator(
171
151
}
172
152
173
153
if ( localGenerators .size () == 1 ) {
174
- final IdentifierGeneratorDefinition generatorDefinition = localGenerators .entrySet ().iterator ().next ().getValue ();
154
+ final IdentifierGeneratorDefinition generatorDefinition =
155
+ localGenerators .values ().iterator ().next ();
175
156
// NOTE : a little bit of a special rule here for the case of just one -
176
157
// we consider it a match, based on strategy, if the strategy is AUTO or matches...
177
158
if ( strategy == AUTO
@@ -181,13 +162,13 @@ private static IdentifierGeneratorDefinition determineImpliedGenerator(
181
162
}
182
163
183
164
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 ) ) {
186
167
if ( matching != null ) {
187
168
// we found multiple matching generators
188
169
return null ;
189
170
}
190
- matching = localGeneratorEntry . getValue () ;
171
+ matching = localGenerator ;
191
172
}
192
173
}
193
174
return matching ;
@@ -259,20 +240,20 @@ private static IdentifierGeneratorDefinition makeIdentifierGeneratorDefinition(
259
240
if ( globalDefinition != null ) {
260
241
return globalDefinition ;
261
242
}
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
+ );
268
256
}
269
-
270
- return IdentifierGeneratorDefinition .createImplicit (
271
- name ,
272
- idAttributeMember .getType (),
273
- generatedValue .generator (),
274
- interpretGenerationType ( generatedValue )
275
- );
276
257
}
277
258
278
259
private static GenerationType interpretGenerationType (GeneratedValue generatedValueAnn ) {
@@ -285,43 +266,35 @@ public static void visitIdGeneratorDefinitions(
285
266
AnnotationTarget annotatedElement ,
286
267
Consumer <IdentifierGeneratorDefinition > consumer ,
287
268
MetadataBuildingContext context ) {
288
- final InFlightMetadataCollector metadataCollector = context . getMetadataCollector ();
289
- final SourceModelBuildingContext sourceModelContext = metadataCollector .getSourceModelBuildingContext ();
269
+ final SourceModelBuildingContext sourceModelContext =
270
+ context . getMetadataCollector () .getSourceModelBuildingContext ();
290
271
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 ) ) );
295
274
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 ) ) );
300
277
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 ) ) );
305
280
306
281
}
307
282
308
283
public static void registerGlobalGenerators (
309
284
AnnotationTarget annotatedElement ,
310
285
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
+ );
313
297
}
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
- );
325
298
}
326
299
327
300
private static IdentifierGeneratorDefinition buildIdGenerator (GenericGenerator generatorAnnotation ) {
@@ -580,12 +553,9 @@ private static <G extends Generator> G instantiateGenerator(
580
553
public static <T extends Generator > T instantiateGenerator (
581
554
BeanContainer beanContainer ,
582
555
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 );
589
559
}
590
560
591
561
/**
@@ -712,19 +682,7 @@ public static void createGeneratorFrom(
712
682
Map <String , Object > configuration ,
713
683
MetadataBuildingContext context ) {
714
684
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 );
728
686
}
729
687
730
688
@@ -802,19 +760,27 @@ private static void setGeneratorCreator(
802
760
identifierValue .setCustomIdGeneratorCreator ( ASSIGNED_IDENTIFIER_GENERATOR_CREATOR );
803
761
}
804
762
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 );
815
764
}
816
765
}
817
766
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
+
818
784
/**
819
785
* Set up the id generator by considering all annotations of the identifier
820
786
* field, including {@linkplain IdGeneratorType id generator annotations},
@@ -890,7 +856,7 @@ static GeneratorCreator createValueGeneratorFromAnnotations(
890
856
}
891
857
892
858
public static void applyIfNotEmpty (String name , String value , BiConsumer <String ,String > consumer ) {
893
- if ( StringHelper . isNotEmpty ( value ) ) {
859
+ if ( isNotEmpty ( value ) ) {
894
860
consumer .accept ( name , value );
895
861
}
896
862
}
0 commit comments