Skip to content

Commit 60c6535

Browse files
committed
HHH-4309 Check entity class generators to allow overriding generators
1 parent b089ce1 commit 60c6535

File tree

5 files changed

+93
-8
lines changed

5 files changed

+93
-8
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/internal/AbstractEntityIdGeneratorResolver.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,24 @@ public AbstractEntityIdGeneratorResolver(
6464
@Override
6565
public final void doSecondPass(Map<String, PersistentClass> persistentClasses) throws MappingException {
6666
switch ( generatedValue.strategy() ) {
67-
case UUID -> GeneratorAnnotationHelper.handleUuidStrategy( idValue, idMember, buildingContext );
67+
case UUID -> handleUuidStrategy();
6868
case IDENTITY -> GeneratorAnnotationHelper.handleIdentityStrategy( idValue );
6969
case SEQUENCE -> handleSequenceStrategy();
7070
case TABLE -> handleTableStrategy();
7171
case AUTO -> handleAutoStrategy();
7272
}
7373
}
7474

75+
private void handleUuidStrategy() {
76+
GeneratorAnnotationHelper.handleUuidStrategy(
77+
idValue,
78+
idMember,
79+
buildingContext.getMetadataCollector().getClassDetailsRegistry()
80+
.getClassDetails( entityMapping.getClassName() ),
81+
buildingContext
82+
);
83+
}
84+
7585
private void handleSequenceStrategy() {
7686
if ( generatedValue.generator().isBlank() ) {
7787
handleUnnamedSequenceGenerator();

hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorAnnotationHelper.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.function.Consumer;
1414
import java.util.function.Function;
1515

16+
import org.checkerframework.checker.nullness.qual.Nullable;
1617
import org.hibernate.annotations.GenericGenerator;
1718
import org.hibernate.annotations.IdGeneratorType;
1819
import org.hibernate.annotations.Parameter;
@@ -61,8 +62,9 @@ public class GeneratorAnnotationHelper {
6162
public static <A extends Annotation> A findLocalizedMatch(
6263
AnnotationDescriptor<A> generatorAnnotationType,
6364
MemberDetails idMember,
64-
Function<A,String> nameExtractor,
65-
String matchName,
65+
ClassDetails entityType,
66+
@Nullable Function<A,String> nameExtractor,
67+
@Nullable String matchName,
6668
MetadataBuildingContext context) {
6769
final SourceModelBuildingContext sourceModelContext =
6870
context.getMetadataCollector().getSourceModelBuildingContext();
@@ -88,7 +90,26 @@ public static <A extends Annotation> A findLocalizedMatch(
8890
}
8991
}
9092

91-
// next, on the class
93+
// next, on the entity class
94+
for ( A generatorAnnotation :
95+
entityType.getRepeatedAnnotationUsages( generatorAnnotationType, sourceModelContext ) ) {
96+
if ( nameExtractor != null ) {
97+
final String registrationName = nameExtractor.apply( generatorAnnotation );
98+
if ( registrationName.isEmpty() ) {
99+
if ( possibleMatch == null ) {
100+
possibleMatch = generatorAnnotation;
101+
}
102+
}
103+
else if ( registrationName.equals( matchName ) ) {
104+
return generatorAnnotation;
105+
}
106+
}
107+
else {
108+
return generatorAnnotation;
109+
}
110+
}
111+
112+
// next, on the declaring class
92113
for ( A generatorAnnotation:
93114
idMember.getDeclaringType().getRepeatedAnnotationUsages( generatorAnnotationType, sourceModelContext ) ) {
94115
if ( nameExtractor != null ) {
@@ -296,10 +317,12 @@ public static <A extends Annotation> void prepareForUse(
296317
public static void handleUuidStrategy(
297318
SimpleValue idValue,
298319
MemberDetails idMember,
320+
ClassDetails entityClass,
299321
MetadataBuildingContext context) {
300322
final org.hibernate.annotations.UuidGenerator generatorConfig = findLocalizedMatch(
301323
HibernateAnnotations.UUID_GENERATOR,
302324
idMember,
325+
entityClass,
303326
null,
304327
null,
305328
context

hibernate-core/src/main/java/org/hibernate/boot/model/internal/IdBagIdGeneratorResolverSecondPass.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ public IdBagIdGeneratorResolverSecondPass(
7171
public void doSecondPass(Map<String, PersistentClass> idGeneratorDefinitionMap) throws MappingException {
7272
final GeneratedValue generatedValue = idBagMember.getDirectAnnotationUsage( GeneratedValue.class );
7373
switch ( generatedValue.strategy() ) {
74-
case UUID -> handleUuidStrategy( idValue, idBagMember, buildingContext );
74+
case UUID -> handleUuidStrategy(
75+
idValue,
76+
idBagMember,
77+
buildingContext.getMetadataCollector().getClassDetailsRegistry()
78+
.getClassDetails( entityMapping.getClassName() ),
79+
buildingContext
80+
);
7581
case IDENTITY -> handleIdentityStrategy( idValue );
7682
case SEQUENCE -> handleSequenceStrategy(
7783
generatorName,
@@ -121,6 +127,8 @@ private void handleTableStrategy(
121127
final TableGenerator localizedTableMatch = findLocalizedMatch(
122128
JpaAnnotations.TABLE_GENERATOR,
123129
idBagMember,
130+
buildingContext.getMetadataCollector().getClassDetailsRegistry()
131+
.getClassDetails( entityMapping.getClassName() ),
124132
TableGenerator::name,
125133
generatorName,
126134
buildingContext
@@ -164,6 +172,8 @@ private void handleSequenceStrategy(
164172
final SequenceGenerator localizedSequencedMatch = findLocalizedMatch(
165173
JpaAnnotations.SEQUENCE_GENERATOR,
166174
idBagMember,
175+
buildingContext.getMetadataCollector().getClassDetailsRegistry()
176+
.getClassDetails( entityMapping.getClassName() ),
167177
SequenceGenerator::name,
168178
generatorName,
169179
buildingContext
@@ -235,6 +245,8 @@ private void handleAutoStrategy(
235245
final SequenceGenerator localizedSequencedMatch = findLocalizedMatch(
236246
JpaAnnotations.SEQUENCE_GENERATOR,
237247
idBagMember,
248+
buildingContext.getMetadataCollector().getClassDetailsRegistry()
249+
.getClassDetails( entityMapping.getClassName() ),
238250
SequenceGenerator::name,
239251
generatorName,
240252
buildingContext
@@ -247,6 +259,8 @@ private void handleAutoStrategy(
247259
final TableGenerator localizedTableMatch = findLocalizedMatch(
248260
JpaAnnotations.TABLE_GENERATOR,
249261
idBagMember,
262+
buildingContext.getMetadataCollector().getClassDetailsRegistry()
263+
.getClassDetails( entityMapping.getClassName() ),
250264
TableGenerator::name,
251265
generatorName,
252266
buildingContext

hibernate-core/src/main/java/org/hibernate/boot/model/internal/IdGeneratorResolverSecondPass.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ protected void handleUnnamedSequenceGenerator() {
5858
final SequenceGenerator localizedMatch = findLocalizedMatch(
5959
JpaAnnotations.SEQUENCE_GENERATOR,
6060
idMember,
61+
buildingContext.getMetadataCollector().getClassDetailsRegistry()
62+
.getClassDetails( entityMapping.getClassName() ),
6163
null,
6264
null,
6365
buildingContext
@@ -77,6 +79,8 @@ protected void handleNamedSequenceGenerator() {
7779
final SequenceGenerator localizedMatch = findLocalizedMatch(
7880
JpaAnnotations.SEQUENCE_GENERATOR,
7981
idMember,
82+
buildingContext.getMetadataCollector().getClassDetailsRegistry()
83+
.getClassDetails( entityMapping.getClassName() ),
8084
SequenceGenerator::name,
8185
generator,
8286
buildingContext
@@ -147,6 +151,8 @@ protected void handleUnnamedTableGenerator() {
147151
final TableGenerator localizedMatch = findLocalizedMatch(
148152
JpaAnnotations.TABLE_GENERATOR,
149153
idMember,
154+
buildingContext.getMetadataCollector().getClassDetailsRegistry()
155+
.getClassDetails( entityMapping.getClassName() ),
150156
null,
151157
null,
152158
buildingContext
@@ -161,6 +167,8 @@ protected void handleNamedTableGenerator() {
161167
final TableGenerator localizedTableMatch = findLocalizedMatch(
162168
JpaAnnotations.TABLE_GENERATOR,
163169
idMember,
170+
buildingContext.getMetadataCollector().getClassDetailsRegistry()
171+
.getClassDetails( entityMapping.getClassName() ),
164172
TableGenerator::name,
165173
generator,
166174
buildingContext
@@ -229,6 +237,8 @@ protected void handleUnnamedAutoGenerator() {
229237
final SequenceGenerator localizedSequenceMatch = findLocalizedMatch(
230238
JpaAnnotations.SEQUENCE_GENERATOR,
231239
idMember,
240+
buildingContext.getMetadataCollector().getClassDetailsRegistry()
241+
.getClassDetails( entityMapping.getClassName() ),
232242
null,
233243
null,
234244
buildingContext
@@ -241,6 +251,8 @@ protected void handleUnnamedAutoGenerator() {
241251
final TableGenerator localizedTableMatch = findLocalizedMatch(
242252
JpaAnnotations.TABLE_GENERATOR,
243253
idMember,
254+
buildingContext.getMetadataCollector().getClassDetailsRegistry()
255+
.getClassDetails( entityMapping.getClassName() ),
244256
null,
245257
null,
246258
buildingContext
@@ -253,6 +265,8 @@ protected void handleUnnamedAutoGenerator() {
253265
final GenericGenerator localizedGenericMatch = findLocalizedMatch(
254266
HibernateAnnotations.GENERIC_GENERATOR,
255267
idMember,
268+
buildingContext.getMetadataCollector().getClassDetailsRegistry()
269+
.getClassDetails( entityMapping.getClassName() ),
256270
null,
257271
null,
258272
buildingContext
@@ -274,7 +288,13 @@ protected void handleUnnamedAutoGenerator() {
274288

275289
if ( idMember.getType().isImplementor( UUID.class )
276290
|| idMember.getType().isImplementor( String.class ) ) {
277-
GeneratorAnnotationHelper.handleUuidStrategy( idValue, idMember, buildingContext );
291+
GeneratorAnnotationHelper.handleUuidStrategy(
292+
idValue,
293+
idMember,
294+
buildingContext.getMetadataCollector().getClassDetailsRegistry()
295+
.getClassDetails( entityMapping.getClassName() ),
296+
buildingContext
297+
);
278298
return;
279299
}
280300

@@ -313,7 +333,13 @@ protected void handleNamedAutoGenerator() {
313333

314334
if ( idMember.getType().isImplementor( UUID.class )
315335
|| idMember.getType().isImplementor( String.class ) ) {
316-
GeneratorAnnotationHelper.handleUuidStrategy( idValue, idMember, buildingContext );
336+
GeneratorAnnotationHelper.handleUuidStrategy(
337+
idValue,
338+
idMember,
339+
buildingContext.getMetadataCollector().getClassDetailsRegistry()
340+
.getClassDetails( entityMapping.getClassName() ),
341+
buildingContext
342+
);
317343
return;
318344
}
319345

@@ -331,6 +357,8 @@ private boolean handleAsLocalAutoGenerator() {
331357
final SequenceGenerator localizedSequenceMatch = findLocalizedMatch(
332358
JpaAnnotations.SEQUENCE_GENERATOR,
333359
idMember,
360+
buildingContext.getMetadataCollector().getClassDetailsRegistry()
361+
.getClassDetails( entityMapping.getClassName() ),
334362
SequenceGenerator::name,
335363
generator,
336364
buildingContext
@@ -343,6 +371,8 @@ private boolean handleAsLocalAutoGenerator() {
343371
final TableGenerator localizedTableMatch = findLocalizedMatch(
344372
JpaAnnotations.TABLE_GENERATOR,
345373
idMember,
374+
buildingContext.getMetadataCollector().getClassDetailsRegistry()
375+
.getClassDetails( entityMapping.getClassName() ),
346376
TableGenerator::name,
347377
generator,
348378
buildingContext
@@ -355,6 +385,8 @@ private boolean handleAsLocalAutoGenerator() {
355385
final GenericGenerator localizedGenericMatch = findLocalizedMatch(
356386
HibernateAnnotations.GENERIC_GENERATOR,
357387
idMember,
388+
buildingContext.getMetadataCollector().getClassDetailsRegistry()
389+
.getClassDetails( entityMapping.getClassName() ),
358390
GenericGenerator::name,
359391
generator,
360392
buildingContext

hibernate-core/src/main/java/org/hibernate/boot/model/internal/StrictIdGeneratorResolverSecondPass.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,13 @@ private void handleAutoGenerator(String globalRegistrationName) {
220220
// Implicit handling of UUID generation
221221
if ( idMember.getType().isImplementor( UUID.class )
222222
|| idMember.getType().isImplementor( String.class ) ) {
223-
handleUuidStrategy( idValue, idMember, buildingContext );
223+
handleUuidStrategy(
224+
idValue,
225+
idMember,
226+
buildingContext.getMetadataCollector().getClassDetailsRegistry()
227+
.getClassDetails( entityMapping.getClassName() ),
228+
buildingContext
229+
);
224230
return;
225231
}
226232

0 commit comments

Comments
 (0)