Skip to content

Commit 3cbd2ae

Browse files
committed
HHH-6488 Implementing SimpleIdentifierSource#getIdentifierGeneratorDescriptor for annotations
1 parent 9bf55b6 commit 3cbd2ae

File tree

10 files changed

+298
-96
lines changed

10 files changed

+298
-96
lines changed

hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/TypeEnumConversionHelper.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,60 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
124
package org.hibernate.metamodel.source.annotations;
225

26+
import javax.persistence.GenerationType;
27+
28+
import org.hibernate.AssertionFailure;
29+
import org.hibernate.id.MultipleHiLoPerTableGenerator;
30+
331
/**
32+
* Helper class which converts type enums from JPA annotations into the Hibernate specific form
33+
*
434
* @author Hardy Ferentschik
535
*/
636
public class TypeEnumConversionHelper {
737
private TypeEnumConversionHelper() {
38+
}
839

40+
public static String generationTypeToGeneratorStrategyName(GenerationType generatorEnum, boolean useNewGeneratorMappings) {
41+
switch ( generatorEnum ) {
42+
case IDENTITY:
43+
return "identity";
44+
case AUTO:
45+
return useNewGeneratorMappings
46+
? "enhanced-sequence"
47+
: "native";
48+
case TABLE:
49+
return useNewGeneratorMappings
50+
? "enhanced-table"
51+
: MultipleHiLoPerTableGenerator.class.getName();
52+
case SEQUENCE:
53+
return useNewGeneratorMappings
54+
? "enhanced-sequence"
55+
: "seqhilo";
56+
}
57+
throw new AssertionFailure( "Unknown GeneratorType: " + generatorEnum );
958
}
1059
}
1160

hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/AssociationAttribute.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.jboss.jandex.DotName;
3535

3636
import org.hibernate.annotations.NotFoundAction;
37+
import org.hibernate.metamodel.source.annotations.AnnotationBindingContext;
3738
import org.hibernate.metamodel.source.annotations.HibernateDotNames;
3839
import org.hibernate.metamodel.source.annotations.JandexHelper;
3940

@@ -50,16 +51,18 @@ public static AssociationAttribute createAssociationAttribute(String name,
5051
Class<?> attributeType,
5152
AttributeType attributeNature,
5253
String accessType,
53-
Map<DotName, List<AnnotationInstance>> annotations) {
54-
return new AssociationAttribute( name, attributeType, attributeNature, accessType, annotations );
54+
Map<DotName, List<AnnotationInstance>> annotations,
55+
AnnotationBindingContext context) {
56+
return new AssociationAttribute( name, attributeType, attributeNature, accessType, annotations, context );
5557
}
5658

5759
private AssociationAttribute(String name,
5860
Class<?> javaType,
5961
AttributeType associationType,
6062
String accessType,
61-
Map<DotName, List<AnnotationInstance>> annotations) {
62-
super( name, javaType, accessType, annotations );
63+
Map<DotName, List<AnnotationInstance>> annotations,
64+
AnnotationBindingContext context) {
65+
super( name, javaType, accessType, annotations, context );
6366
this.associationType = associationType;
6467
this.ignoreNotFound = ignoreNotFound();
6568

hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/BasicAttribute.java

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,23 @@
2828
import java.util.List;
2929
import java.util.Map;
3030
import javax.persistence.FetchType;
31+
import javax.persistence.GenerationType;
3132

3233
import org.jboss.jandex.AnnotationInstance;
3334
import org.jboss.jandex.AnnotationValue;
3435
import org.jboss.jandex.DotName;
3536

3637
import org.hibernate.AnnotationException;
3738
import org.hibernate.annotations.GenerationTime;
39+
import org.hibernate.internal.util.StringHelper;
3840
import org.hibernate.mapping.PropertyGeneration;
41+
import org.hibernate.metamodel.binding.IdGenerator;
42+
import org.hibernate.metamodel.source.MappingException;
43+
import org.hibernate.metamodel.source.annotations.AnnotationBindingContext;
3944
import org.hibernate.metamodel.source.annotations.HibernateDotNames;
4045
import org.hibernate.metamodel.source.annotations.JPADotNames;
4146
import org.hibernate.metamodel.source.annotations.JandexHelper;
47+
import org.hibernate.metamodel.source.annotations.TypeEnumConversionHelper;
4248

4349
/**
4450
* Represent a mapped attribute (explicitly or implicitly mapped).
@@ -51,6 +57,12 @@ public class BasicAttribute extends MappedAttribute {
5157
*/
5258
private final boolean isId;
5359

60+
/**
61+
* The id generator in case this basic attribute represents an simple id. Will be {@code null} in case there
62+
* is no explicit id generator or the containing entity does not have a simple id
63+
*/
64+
private final IdGenerator idGenerator;
65+
5466
/**
5567
* Is this a versioned property (annotated w/ {@code @Version}.
5668
*/
@@ -88,12 +100,20 @@ public class BasicAttribute extends MappedAttribute {
88100
private final String customReadFragment;
89101
private final String checkCondition;
90102

91-
public static BasicAttribute createSimpleAttribute(String name, Class<?> attributeType, Map<DotName, List<AnnotationInstance>> annotations, String accessType) {
92-
return new BasicAttribute( name, attributeType, accessType, annotations );
103+
public static BasicAttribute createSimpleAttribute(String name,
104+
Class<?> attributeType,
105+
Map<DotName, List<AnnotationInstance>> annotations,
106+
String accessType,
107+
AnnotationBindingContext context) {
108+
return new BasicAttribute( name, attributeType, accessType, annotations, context );
93109
}
94110

95-
BasicAttribute(String name, Class<?> attributeType, String accessType, Map<DotName, List<AnnotationInstance>> annotations) {
96-
super( name, attributeType, accessType, annotations );
111+
BasicAttribute(String name,
112+
Class<?> attributeType,
113+
String accessType,
114+
Map<DotName, List<AnnotationInstance>> annotations,
115+
AnnotationBindingContext context) {
116+
super( name, attributeType, accessType, annotations, context );
97117

98118
AnnotationInstance idAnnotation = JandexHelper.getSingleAnnotation( annotations, JPADotNames.ID );
99119
AnnotationInstance embeddedIdAnnotation = JandexHelper.getSingleAnnotation(
@@ -112,6 +132,10 @@ public static BasicAttribute createSimpleAttribute(String name, Class<?> attribu
112132
// an id must be unique and cannot be nullable
113133
columnValues.setUnique( true );
114134
columnValues.setNullable( false );
135+
idGenerator = checkGeneratedValueAnnotation();
136+
}
137+
else {
138+
idGenerator = null;
115139
}
116140

117141
this.isOptimisticLockable = checkOptimisticLockAnnotation();
@@ -175,6 +199,10 @@ public String getCheckCondition() {
175199
return checkCondition;
176200
}
177201

202+
public IdGenerator getIdGenerator() {
203+
return idGenerator;
204+
}
205+
178206
@Override
179207
public String toString() {
180208
final StringBuilder sb = new StringBuilder();
@@ -290,6 +318,36 @@ private String parseCheckAnnotation() {
290318
}
291319
return checkCondition;
292320
}
321+
322+
private IdGenerator checkGeneratedValueAnnotation() {
323+
IdGenerator generator = null;
324+
AnnotationInstance generatedValueAnnotation = JandexHelper.getSingleAnnotation(
325+
annotations(),
326+
JPADotNames.GENERATED_VALUE
327+
);
328+
if ( generatedValueAnnotation != null ) {
329+
String name = JandexHelper.getValue( generatedValueAnnotation, "generator", String.class );
330+
if ( StringHelper.isNotEmpty( name ) ) {
331+
generator = getContext().getMetadataImplementor().getIdGenerator( name );
332+
if ( generator == null ) {
333+
throw new MappingException( String.format( "Unable to find named generator %s", name ), null );
334+
}
335+
}
336+
else {
337+
GenerationType genType = JandexHelper.getEnumValue(
338+
generatedValueAnnotation,
339+
"strategy",
340+
GenerationType.class
341+
);
342+
String strategy = TypeEnumConversionHelper.generationTypeToGeneratorStrategyName(
343+
genType,
344+
getContext().getMetadataImplementor().getOptions().useNewIdentifierGenerators()
345+
);
346+
generator = new IdGenerator( null, strategy, null );
347+
}
348+
}
349+
return generator;
350+
}
293351
}
294352

295353

hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/MappedAttribute.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.jboss.jandex.AnnotationValue;
3232
import org.jboss.jandex.DotName;
3333

34+
import org.hibernate.metamodel.source.annotations.AnnotationBindingContext;
3435
import org.hibernate.metamodel.source.annotations.HibernateDotNames;
3536
import org.hibernate.metamodel.source.annotations.JandexHelper;
3637

@@ -71,13 +72,19 @@ public abstract class MappedAttribute implements Comparable<MappedAttribute> {
7172
*/
7273
private final Map<String, String> explicitHibernateTypeParameters;
7374

74-
MappedAttribute(String name, Class<?> attributeType, String accessType, Map<DotName, List<AnnotationInstance>> annotations) {
75+
private final AnnotationBindingContext context;
76+
77+
MappedAttribute(String name, Class<?> attributeType, String accessType, Map<DotName, List<AnnotationInstance>> annotations, AnnotationBindingContext context) {
78+
this.context = context;
7579
this.annotations = annotations;
7680
this.name = name;
7781
this.attributeType = attributeType;
7882
this.accessType = accessType;
7983

80-
final AnnotationInstance typeAnnotation = JandexHelper.getSingleAnnotation(annotations(), HibernateDotNames.TYPE );
84+
final AnnotationInstance typeAnnotation = JandexHelper.getSingleAnnotation(
85+
annotations(),
86+
HibernateDotNames.TYPE
87+
);
8188
if ( typeAnnotation != null ) {
8289
this.explicitHibernateTypeName = typeAnnotation.value( "type" ).asString();
8390
this.explicitHibernateTypeParameters = extractTypeParameters( typeAnnotation );
@@ -123,6 +130,10 @@ public Map<String, String> getExplicitHibernateTypeParameters() {
123130
return explicitHibernateTypeParameters;
124131
}
125132

133+
public AnnotationBindingContext getContext() {
134+
return context;
135+
}
136+
126137
Map<DotName, List<AnnotationInstance>> annotations() {
127138
return annotations;
128139
}

hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/attribute/SimpleIdentifierSourceImpl.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package org.hibernate.metamodel.source.annotations.attribute;
2525

26+
import org.hibernate.AssertionFailure;
2627
import org.hibernate.metamodel.binding.IdGenerator;
2728
import org.hibernate.metamodel.source.binder.SimpleIdentifierSource;
2829
import org.hibernate.metamodel.source.binder.SingularAttributeSource;
@@ -34,6 +35,14 @@ public class SimpleIdentifierSourceImpl implements SimpleIdentifierSource {
3435
private final BasicAttribute attribute;
3536

3637
public SimpleIdentifierSourceImpl(BasicAttribute attribute) {
38+
if ( !attribute.isId() ) {
39+
throw new AssertionFailure(
40+
String.format(
41+
"A non id attribute was passed to SimpleIdentifierSourceImpl: %s",
42+
attribute.toString()
43+
)
44+
);
45+
}
3746
this.attribute = attribute;
3847
}
3948

@@ -49,7 +58,7 @@ public SingularAttributeSource getIdentifierAttributeSource() {
4958

5059
@Override
5160
public IdGenerator getIdentifierGeneratorDescriptor() {
52-
return null; //To change body of implemented methods use File | Settings | File Templates.
61+
return attribute.getIdGenerator();
5362
}
5463
}
5564

hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClass.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
import org.hibernate.metamodel.source.annotations.attribute.AssociationAttribute;
6161
import org.hibernate.metamodel.source.annotations.attribute.AttributeOverride;
6262
import org.hibernate.metamodel.source.annotations.attribute.AttributeType;
63-
import org.hibernate.metamodel.source.annotations.attribute.MappedAttribute;
6463
import org.hibernate.metamodel.source.annotations.attribute.BasicAttribute;
64+
import org.hibernate.metamodel.source.annotations.attribute.MappedAttribute;
6565

6666
/**
6767
* Base class for a configured entity, mapped super class or embeddable
@@ -246,7 +246,7 @@ private AccessType determineClassAccessType(AccessType defaultAccessType) {
246246

247247
AnnotationInstance accessAnnotation = JandexHelper.getSingleAnnotation( classInfo, JPADotNames.ACCESS );
248248
if ( accessAnnotation != null && accessAnnotation.target().getClass().equals( ClassInfo.class ) ) {
249-
accessType = JandexHelper.getValueAsEnum( accessAnnotation, "value", AccessType.class );
249+
accessType = JandexHelper.getEnumValue( accessAnnotation, "value", AccessType.class );
250250
}
251251

252252
return accessType;
@@ -334,7 +334,7 @@ private Set<String> createExplicitlyConfiguredAccessProperties(ResolvedTypeWithM
334334
continue;
335335
}
336336

337-
AccessType accessType = JandexHelper.getValueAsEnum( accessAnnotation, "value", AccessType.class );
337+
AccessType accessType = JandexHelper.getEnumValue( accessAnnotation, "value", AccessType.class );
338338

339339
if ( !isExplicitAttributeAccessAnnotationPlacedCorrectly( annotationTarget, accessType ) ) {
340340
continue;
@@ -446,7 +446,7 @@ private void createMappedAttribute(Member member, ResolvedTypeWithMembers resolv
446446
switch ( attributeNature ) {
447447
case BASIC: {
448448
BasicAttribute attribute = BasicAttribute.createSimpleAttribute(
449-
attributeName, attributeType, annotations, accessTypeString
449+
attributeName, attributeType, annotations, accessTypeString, getContext()
450450
);
451451
if ( attribute.isId() ) {
452452
idAttributeMap.put( attributeName, attribute );
@@ -469,7 +469,7 @@ else if ( attribute.isVersioned() ) {
469469
// TODO handle the different association types
470470
default: {
471471
AssociationAttribute attribute = AssociationAttribute.createAssociationAttribute(
472-
attributeName, attributeType, attributeNature, accessTypeString, annotations
472+
attributeName, attributeType, attributeNature, accessTypeString, annotations, getContext()
473473
);
474474
associationAttributeMap.put( attributeName, attribute );
475475
}

0 commit comments

Comments
 (0)