Skip to content

Commit 4781983

Browse files
committed
HHH-18705 Quick & ugly fix to the problem
Ugly copy & paste from org.hibernate.processor.annotation.NamedQueryMethod
1 parent 2f0b5da commit 4781983

File tree

1 file changed

+44
-20
lines changed
  • tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation

1 file changed

+44
-20
lines changed

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

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@
1313
import org.hibernate.processor.util.Constants;
1414
import org.hibernate.processor.validation.ProcessorSessionFactory;
1515
import org.hibernate.processor.validation.Validation;
16-
import org.hibernate.query.criteria.JpaEntityJoin;
17-
import org.hibernate.query.criteria.JpaRoot;
18-
import org.hibernate.query.criteria.JpaSelection;
16+
import org.hibernate.query.sqm.SqmExpressible;
1917
import org.hibernate.query.sqm.tree.SqmStatement;
20-
import org.hibernate.query.sqm.tree.select.SqmSelectClause;
2118
import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
19+
import org.hibernate.query.sqm.tree.select.SqmSelectableNode;
20+
import org.hibernate.type.descriptor.java.JavaType;
2221

2322
import javax.lang.model.element.AnnotationMirror;
2423
import javax.lang.model.element.AnnotationValue;
2524
import javax.lang.model.element.Element;
25+
import javax.lang.model.element.ModuleElement;
26+
import javax.lang.model.element.TypeElement;
27+
import javax.lang.model.util.Elements;
2628
import java.util.List;
2729

2830
import static java.lang.Character.isJavaIdentifierStart;
@@ -31,6 +33,7 @@
3133
import static org.hibernate.processor.util.TypeUtils.containsAnnotation;
3234
import static org.hibernate.processor.util.TypeUtils.getAnnotationMirror;
3335
import static org.hibernate.processor.util.TypeUtils.getAnnotationValue;
36+
import static org.hibernate.processor.validation.ProcessorSessionFactory.findEntityByUnqualifiedName;
3437

3538
public abstract class AnnotationMeta implements Metamodel {
3639

@@ -136,24 +139,23 @@ && getAnnotationValue( mirror, "resultClass" ) == null ) {
136139
}
137140
}
138141

139-
private static @Nullable String resultType(SqmSelectStatement<?> selectStatement) {
140-
final JpaSelection<?> selection = selectStatement.getSelection();
141-
if (selection == null) {
142-
return null;
143-
}
144-
else if (selection instanceof SqmSelectClause from) {
145-
return from.getSelectionItems().size() > 1
146-
? "Object[]"
147-
: from.getSelectionItems().get(0).getJavaTypeName();
148-
}
149-
else if (selection instanceof JpaRoot<?> root) {
150-
return root.getModel().getTypeName();
151-
}
152-
else if (selection instanceof JpaEntityJoin<?, ?> join) {
153-
return join.getModel().getTypeName();
142+
private @Nullable String resultType(SqmSelectStatement<?> selectStatement) {
143+
final JavaType<?> javaType = selectStatement.getSelection().getJavaTypeDescriptor();
144+
if ( javaType != null ) {
145+
return javaType.getTypeName();
154146
}
155147
else {
156-
return selection.getJavaTypeName();
148+
final List<SqmSelectableNode<?>> items =
149+
selectStatement.getQuerySpec().getSelectClause().getSelectionItems();
150+
final SqmExpressible<?> expressible;
151+
if ( items.size() == 1 && ( expressible = items.get( 0 ).getExpressible() ) != null ) {
152+
final String typeName = expressible.getTypeName();
153+
final TypeElement entityType = entityType( typeName );
154+
return entityType == null ? typeName : entityType.getQualifiedName().toString();
155+
}
156+
else {
157+
return "Object[]";
158+
}
157159
}
158160
}
159161

@@ -283,4 +285,26 @@ public void syntaxError(
283285
}
284286
}
285287
}
288+
289+
private @Nullable TypeElement entityType(String entityName) {
290+
final Context context = getContext();
291+
final Elements elementUtils = context.getElementUtils();
292+
final String qualifiedName = context.qualifiedNameForEntityName(entityName);
293+
if ( qualifiedName != null ) {
294+
return elementUtils.getTypeElement(qualifiedName);
295+
}
296+
TypeElement symbol =
297+
findEntityByUnqualifiedName( entityName,
298+
elementUtils.getModuleElement("") );
299+
if ( symbol != null ) {
300+
return symbol;
301+
}
302+
for ( ModuleElement module : elementUtils.getAllModuleElements() ) {
303+
symbol = findEntityByUnqualifiedName( entityName, module );
304+
if ( symbol != null ) {
305+
return symbol;
306+
}
307+
}
308+
return null;
309+
}
286310
}

0 commit comments

Comments
 (0)