Skip to content

Commit e13efce

Browse files
committed
HHH-18649 TypedQueryReference and EntityGraph in static metamodel
1 parent d534989 commit e13efce

File tree

5 files changed

+101
-31
lines changed

5 files changed

+101
-31
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,7 @@ else if ( hasAuxiliaryAnnotations( element ) ) {
376376
context.logMessage( Diagnostic.Kind.OTHER, "Processing annotated class '" + element + "'" );
377377
handleRootElementAuxiliaryAnnotationMirrors( element );
378378
}
379-
else if ( element instanceof TypeElement ) {
380-
final TypeElement typeElement = (TypeElement) element;
379+
else if ( element instanceof TypeElement typeElement ) {
381380
final AnnotationMirror repository = getAnnotationMirror( element, JD_REPOSITORY );
382381
if ( repository != null ) {
383382
final AnnotationValue provider = getAnnotationValue( repository, "provider" );

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@
2121
import javax.lang.model.element.Element;
2222
import java.util.List;
2323

24+
import static java.lang.Character.isJavaIdentifierStart;
25+
import static org.hibernate.processor.util.Constants.JAVA_OBJECT;
26+
import static org.hibernate.processor.util.Constants.NAMED_QUERY;
2427
import static org.hibernate.processor.util.TypeUtils.containsAnnotation;
2528
import static org.hibernate.processor.util.TypeUtils.getAnnotationMirror;
2629
import static org.hibernate.processor.util.TypeUtils.getAnnotationValue;
2730

2831
public abstract class AnnotationMeta implements Metamodel {
2932

3033
void addAuxiliaryMembers() {
31-
addAuxiliaryMembersForAnnotation( Constants.NAMED_QUERY, "QUERY_" );
34+
addAuxiliaryMembersForAnnotation( NAMED_QUERY, "QUERY_" );
3235
addAuxiliaryMembersForRepeatableAnnotation( Constants.NAMED_QUERIES, "QUERY_" );
3336
addAuxiliaryMembersForAnnotation( Constants.NAMED_NATIVE_QUERY, "QUERY_" );
3437
addAuxiliaryMembersForRepeatableAnnotation( Constants.NAMED_NATIVE_QUERIES, "QUERY_" );
@@ -50,7 +53,7 @@ void addAuxiliaryMembers() {
5053
void checkNamedQueries() {
5154
boolean checkHql = containsAnnotation( getElement(), Constants.CHECK_HQL )
5255
|| containsAnnotation( getElement().getEnclosingElement(), Constants.CHECK_HQL );
53-
handleNamedQueryAnnotation( Constants.NAMED_QUERY, checkHql );
56+
handleNamedQueryAnnotation( NAMED_QUERY, checkHql );
5457
handleNamedQueryRepeatableAnnotation( Constants.NAMED_QUERIES, checkHql );
5558
handleNamedQueryAnnotation( Constants.HIB_NAMED_QUERY, checkHql );
5659
handleNamedQueryRepeatableAnnotation( Constants.HIB_NAMED_QUERIES, checkHql );
@@ -86,9 +89,7 @@ private void handleNamedQuery(AnnotationMirror mirror, boolean checkHql) {
8689
final boolean reportErrors = context.checkNamedQuery( name );
8790
final AnnotationValue value = getAnnotationValue( mirror, "query" );
8891
if ( value != null ) {
89-
final Object query = value.getValue();
90-
if ( query instanceof String ) {
91-
final String hql = (String) query;
92+
if ( value.getValue() instanceof String hql ) {
9293
final SqmStatement<?> statement =
9394
Validation.validate(
9495
hql,
@@ -124,7 +125,7 @@ && isQueryMethodName( name ) ) {
124125
private static boolean isQueryMethodName(String name) {
125126
return name.length() >= 2
126127
&& name.charAt(0) == '#'
127-
&& Character.isJavaIdentifierStart( name.charAt(1) )
128+
&& isJavaIdentifierStart( name.charAt(1) )
128129
&& name.substring(2).chars().allMatch(Character::isJavaIdentifierPart);
129130
}
130131

@@ -155,13 +156,30 @@ private void addAuxiliaryMembersForMirror(AnnotationMirror mirror, String prefix
155156
if ( key.getSimpleName().contentEquals("name") ) {
156157
final String name = value.getValue().toString();
157158
if ( !name.isEmpty() ) {
158-
putMember( prefix + name,
159-
new NameMetaAttribute( this, name, prefix ) );
159+
putMember( prefix + name, auxiliaryMember( mirror, prefix, name ) );
160160
}
161161
}
162162
});
163163
}
164164

165+
private NameMetaAttribute auxiliaryMember(AnnotationMirror mirror, String prefix, String name) {
166+
if ( !isJakartaDataStyle() && "QUERY_".equals(prefix) ) {
167+
final AnnotationValue resultClass = getAnnotationValue( mirror, "resultClass" );
168+
//TODO: if there is no explicit result class, obtain the result class by
169+
// type-checking the query (this is allowed but not required by JPA)
170+
return new TypedMetaAttribute( this, name, prefix,
171+
resultClass == null ? JAVA_OBJECT : resultClass.getValue().toString(),
172+
"jakarta.persistence.TypedQueryReference" );
173+
}
174+
else if ( !isJakartaDataStyle() && "GRAPH_".equals(prefix) ) {
175+
return new TypedMetaAttribute( this, name, prefix, getQualifiedName(),
176+
"jakarta.persistence.EntityGraph" );
177+
}
178+
else {
179+
return new NameMetaAttribute( this, name, prefix);
180+
}
181+
}
182+
165183
protected String getSessionVariableName() {
166184
return "entityManager";
167185
}

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,20 @@ public boolean hasStringAttribute() {
4545
public String getAttributeDeclarationString() {
4646
return new StringBuilder()
4747
.append("\n/**\n * @see ")
48-
.append( parent.getQualifiedName() )
49-
.append( "#")
50-
.append( element.getSimpleName() )
51-
.append( "\n **/\n" )
52-
.append( "public static volatile " )
53-
.append( parent.importType( getMetaType() ) )
54-
.append( "<" )
55-
.append( parent.importType( parent.getQualifiedName() ) )
56-
.append( ", " )
57-
.append( parent.importType( getTypeDeclaration() ) )
58-
.append( "> " )
59-
.append( getPropertyName() )
60-
.append( ";" )
48+
.append(parent.getQualifiedName())
49+
.append('#')
50+
.append(element.getSimpleName())
51+
.append("\n **/\n")
52+
.append("public static volatile ")
53+
.append(parent.importType(getMetaType()))
54+
.append('<')
55+
.append(parent.importType(parent.getQualifiedName()))
56+
.append(", ")
57+
.append(parent.importType(getTypeDeclaration()))
58+
.append('>')
59+
.append(' ')
60+
.append(getPropertyName())
61+
.append(';')
6162
.toString();
6263
}
6364

@@ -66,13 +67,13 @@ public String getAttributeNameDeclarationString(){
6667
return new StringBuilder()
6768
.append("public static final ")
6869
.append(parent.importType(String.class.getName()))
69-
.append(" ")
70+
.append(' ')
7071
.append(getUpperUnderscoreCaseFromLowerCamelCase(getPropertyName()))
7172
.append(" = ")
72-
.append("\"")
73+
.append('"')
7374
.append(getPropertyName())
74-
.append("\"")
75-
.append(";")
75+
.append('"')
76+
.append(';')
7677
.toString();
7778
}
7879

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ public String getAttributeNameDeclarationString() {
4343
return new StringBuilder()
4444
.append("public static final ")
4545
.append(annotationMetaEntity.importType(String.class.getName()))
46-
.append(" ")
46+
.append(' ')
4747
.append(prefix)
4848
.append(fieldName())
4949
.append(" = ")
50-
.append("\"")
50+
.append('"')
5151
.append(name)
52-
.append("\"")
53-
.append(";")
52+
.append('"')
53+
.append(';')
5454
.toString();
5555
}
5656

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
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+
* @author Gavin King
13+
*/
14+
class TypedMetaAttribute extends NameMetaAttribute {
15+
private final String resultType;
16+
private final String referenceType;
17+
18+
public TypedMetaAttribute(
19+
Metamodel annotationMetaEntity,
20+
String name,
21+
String prefix,
22+
String resultType,
23+
String referenceType) {
24+
super( annotationMetaEntity, name, prefix );
25+
this.resultType = resultType;
26+
this.referenceType = referenceType;
27+
}
28+
29+
@Override
30+
public boolean hasTypedAttribute() {
31+
return true;
32+
}
33+
34+
@Override
35+
public String getAttributeDeclarationString() {
36+
final Metamodel entity = getHostingEntity();
37+
return new StringBuilder()
38+
.append("\n/**\n * @see ")
39+
.append(entity.getQualifiedName())
40+
.append("\n **/\n")
41+
.append("public static volatile ")
42+
.append(entity.importType(referenceType))
43+
.append('<')
44+
.append(entity.importType(resultType))
45+
.append('>')
46+
.append(' ')
47+
.append('_')
48+
.append(nameToMethodName(getPropertyName()))
49+
.append(';')
50+
.toString();
51+
}
52+
}

0 commit comments

Comments
 (0)