Skip to content

Commit c9c542c

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

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;
@@ -33,6 +35,7 @@
3335
import static org.hibernate.processor.util.TypeUtils.containsAnnotation;
3436
import static org.hibernate.processor.util.TypeUtils.getAnnotationMirror;
3537
import static org.hibernate.processor.util.TypeUtils.getAnnotationValue;
38+
import static org.hibernate.processor.validation.ProcessorSessionFactory.findEntityByUnqualifiedName;
3639

3740
public abstract class AnnotationMeta implements Metamodel {
3841

@@ -139,24 +142,23 @@ private void handleNamedQuery(AnnotationMirror mirror, boolean checkHql) {
139142
}
140143
}
141144

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

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

0 commit comments

Comments
 (0)