Skip to content

Commit 0b035dc

Browse files
cigalygavinking
authored andcommitted
HHH-18958 If NamedQuery.returnClass attribute is present use it when generating method return type
1 parent 3d35dbf commit 0b035dc

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,20 @@ private void handleNamedQuery(AnnotationMirror mirror, boolean checkHql) {
110110
if ( !isJakartaDataStyle()
111111
&& statement instanceof SqmSelectStatement<?> selectStatement ) {
112112
if ( isQueryMethodName( name ) ) {
113+
final AnnotationValue annotationValue = getAnnotationValue( mirror, "resultClass" );
114+
final String resultType = annotationValue != null
115+
? annotationValue.getValue().toString()
116+
: resultType( selectStatement, context );
113117
putMember( name,
114-
// TODO: respect @NamedQuery(resultClass)
115118
new NamedQueryMethod(
116119
this,
117120
selectStatement,
118121
name.substring(1),
119122
isRepository(),
120123
getSessionType(),
121124
getSessionVariableName(),
122-
context.addNonnullAnnotation()
125+
context.addNonnullAnnotation(),
126+
resultType
123127
)
124128
);
125129
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.util.TreeSet;
1616

1717
import static org.hibernate.processor.util.StringUtil.nameToFieldName;
18-
import static org.hibernate.processor.util.SqmTypeUtils.resultType;
1918

2019
/**
2120
* @author Gavin King
@@ -28,6 +27,7 @@ class NamedQueryMethod implements MetaAttribute {
2827
private final boolean reactive;
2928
private final String sessionVariableName;
3029
private final boolean addNonnullAnnotation;
30+
private final String resultClass;
3131

3232
public NamedQueryMethod(
3333
AnnotationMeta annotationMeta,
@@ -36,14 +36,16 @@ public NamedQueryMethod(
3636
boolean belongsToRepository,
3737
@Nullable String sessionType,
3838
String sessionVariableName,
39-
boolean addNonnullAnnotation) {
39+
boolean addNonnullAnnotation,
40+
String resultClass) {
4041
this.annotationMeta = annotationMeta;
4142
this.select = select;
4243
this.name = name;
4344
this.belongsToRepository = belongsToRepository;
4445
this.reactive = Constants.MUTINY_SESSION.equals(sessionType);
4546
this.sessionVariableName = sessionVariableName;
4647
this.addNonnullAnnotation = addNonnullAnnotation;
48+
this.resultClass = resultClass;
4749
}
4850

4951
@Override
@@ -72,7 +74,7 @@ public String getAttributeDeclarationString() {
7274
.append(".createNamedQuery(")
7375
.append(fieldName())
7476
.append(", ")
75-
.append( annotationMeta.importType( resultType( select, annotationMeta.getContext() ) ) )
77+
.append( annotationMeta.importType( resultClass ) )
7678
.append( ".class)");
7779
for ( SqmParameter<?> param : sortedParameters ) {
7880
declaration
@@ -123,7 +125,7 @@ private void returnType(StringBuilder declaration) {
123125
declaration
124126
.append(annotationMeta.importType(Constants.LIST))
125127
.append('<')
126-
.append( annotationMeta.importType( resultType( select, annotationMeta.getContext() ) ) )
128+
.append( annotationMeta.importType( resultClass ) )
127129
.append("> ")
128130
.append(name);
129131
if ( reactive ) {

0 commit comments

Comments
 (0)