Skip to content

Commit 5684680

Browse files
committed
HHH-19450 generated typesafe references to EnabledFetchProfile objects
1 parent a2d5ca0 commit 5684680

File tree

3 files changed

+102
-16
lines changed

3 files changed

+102
-16
lines changed

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMeta.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import static java.lang.Character.isJavaIdentifierStart;
2525
import static org.hibernate.processor.util.Constants.ENTITY_GRAPH;
26+
import static org.hibernate.processor.util.Constants.HIB_ENABLED_FETCH_PROFILE;
2627
import static org.hibernate.processor.util.Constants.JAVA_OBJECT;
2728
import static org.hibernate.processor.util.Constants.NAMED_QUERY;
2829
import static org.hibernate.processor.util.Constants.TYPED_QUERY_REFERENCE;
@@ -182,22 +183,26 @@ private void addAuxiliaryMembersForMirror(AnnotationMirror mirror, String prefix
182183
}
183184

184185
private NameMetaAttribute auxiliaryMember(AnnotationMirror mirror, String prefix, String name) {
185-
if ( "QUERY_".equals(prefix) ) {
186-
final AnnotationValue resultClass = getAnnotationValue( mirror, "resultClass" );
187-
// if there is no explicit result class, we will infer it later by
188-
// type checking the query (this is allowed but not required by JPA)
189-
// and then we will replace this TypedMetaAttribute
190-
return new TypedMetaAttribute( this, name, prefix,
191-
resultClass == null ? JAVA_OBJECT : resultClass.getValue().toString(),
192-
TYPED_QUERY_REFERENCE, null );
193-
}
194-
else if ( "GRAPH_".equals(prefix) ) {
195-
return new TypedMetaAttribute( this, name, prefix, getQualifiedName(),
196-
ENTITY_GRAPH, null );
197-
}
198-
else {
199-
return new NameMetaAttribute( this, name, prefix);
200-
}
186+
return switch (prefix) {
187+
case "QUERY_" -> {
188+
final AnnotationValue resultClass = getAnnotationValue( mirror, "resultClass" );
189+
// if there is no explicit result class, we will infer it later by
190+
// type checking the query (this is allowed but not required by JPA)
191+
// and then we will replace this TypedMetaAttribute
192+
final String resultTypeName =
193+
resultClass == null ? JAVA_OBJECT : resultClass.getValue().toString();
194+
yield new TypedMetaAttribute( this, name, prefix, resultTypeName,
195+
TYPED_QUERY_REFERENCE, null );
196+
}
197+
case "GRAPH_" ->
198+
new TypedMetaAttribute( this, name, prefix, getQualifiedName(),
199+
ENTITY_GRAPH, null );
200+
case "PROFILE_" ->
201+
new EnabledFetchProfileMetaAttribute( this, name, prefix,
202+
HIB_ENABLED_FETCH_PROFILE );
203+
default ->
204+
new NameMetaAttribute( this, name, prefix );
205+
};
201206
}
202207

203208
protected String getSessionVariableName() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.processor.annotation;
6+
7+
import org.hibernate.processor.model.Metamodel;
8+
9+
import static org.hibernate.processor.util.StringUtil.nameToMethodName;
10+
11+
/**
12+
* Represents a named fetch profile.
13+
*
14+
* @author Gavin King
15+
*/
16+
class EnabledFetchProfileMetaAttribute extends NameMetaAttribute {
17+
private final String prefix;
18+
private final String referenceType;
19+
20+
public EnabledFetchProfileMetaAttribute(
21+
Metamodel annotationMetaEntity,
22+
String name,
23+
String prefix,
24+
String referenceType) {
25+
super( annotationMetaEntity, name, prefix );
26+
this.prefix = prefix;
27+
this.referenceType = referenceType;
28+
}
29+
30+
@Override
31+
public boolean hasTypedAttribute() {
32+
return true;
33+
}
34+
35+
@Override
36+
public String getAttributeNameDeclarationString() {
37+
StringBuilder declaration = new StringBuilder();
38+
declaration
39+
.append("\n/**\n * @see ")
40+
.append("#");
41+
appendFieldName( declaration );
42+
return declaration
43+
.append( "\n **/\n" )
44+
.append(super.getAttributeNameDeclarationString())
45+
.toString();
46+
}
47+
48+
@Override
49+
public String getAttributeDeclarationString() {
50+
final Metamodel entity = getHostingEntity();
51+
final StringBuilder declaration = new StringBuilder();
52+
declaration
53+
.append("\n/**")
54+
.append("\n * The fetch profile named {@value ")
55+
.append(prefix)
56+
.append(fieldName())
57+
.append("}\n")
58+
.append(" *\n * @see ")
59+
.append(entity.getQualifiedName())
60+
.append("\n **/\n")
61+
.append("public static final ")
62+
.append(entity.importType(referenceType))
63+
.append(' ');
64+
appendFieldName( declaration );
65+
declaration
66+
.append(" = new ")
67+
.append(entity.importType(referenceType))
68+
.append("(")
69+
.append(prefix)
70+
.append(fieldName())
71+
.append(");");
72+
return declaration.toString();
73+
}
74+
75+
private void appendFieldName(StringBuilder declaration) {
76+
declaration
77+
.append('_')
78+
.append(nameToMethodName(getPropertyName()));
79+
}
80+
}

tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public final class Constants {
109109
public static final String MUTINY_SESSION = "org.hibernate.reactive.mutiny.Mutiny.Session";
110110
public static final String MUTINY_STATELESS_SESSION = "org.hibernate.reactive.mutiny.Mutiny.StatelessSession";
111111
public static final String QUARKUS_SESSION_OPERATIONS = "io.quarkus.hibernate.reactive.panache.common.runtime.SessionOperations";
112+
public static final String HIB_ENABLED_FETCH_PROFILE = "org.hibernate.EnabledFetchProfile";
112113

113114
public static final String TUPLE = "jakarta.persistence.Tuple";
114115

0 commit comments

Comments
 (0)