2121import javax .lang .model .element .Element ;
2222import 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 ;
2427import static org .hibernate .processor .util .TypeUtils .containsAnnotation ;
2528import static org .hibernate .processor .util .TypeUtils .getAnnotationMirror ;
2629import static org .hibernate .processor .util .TypeUtils .getAnnotationValue ;
2730
2831public 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 }
0 commit comments