|
13 | 13 | import org.hibernate.processor.util.Constants; |
14 | 14 | import org.hibernate.processor.validation.ProcessorSessionFactory; |
15 | 15 | 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; |
19 | 17 | import org.hibernate.query.sqm.tree.SqmStatement; |
20 | | -import org.hibernate.query.sqm.tree.select.SqmSelectClause; |
21 | 18 | 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; |
22 | 21 |
|
23 | 22 | import javax.lang.model.element.AnnotationMirror; |
24 | 23 | import javax.lang.model.element.AnnotationValue; |
25 | 24 | 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; |
26 | 28 | import java.util.List; |
27 | 29 |
|
28 | 30 | import static java.lang.Character.isJavaIdentifierStart; |
|
33 | 35 | import static org.hibernate.processor.util.TypeUtils.containsAnnotation; |
34 | 36 | import static org.hibernate.processor.util.TypeUtils.getAnnotationMirror; |
35 | 37 | import static org.hibernate.processor.util.TypeUtils.getAnnotationValue; |
| 38 | +import static org.hibernate.processor.validation.ProcessorSessionFactory.findEntityByUnqualifiedName; |
36 | 39 |
|
37 | 40 | public abstract class AnnotationMeta implements Metamodel { |
38 | 41 |
|
@@ -139,24 +142,23 @@ private void handleNamedQuery(AnnotationMirror mirror, boolean checkHql) { |
139 | 142 | } |
140 | 143 | } |
141 | 144 |
|
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(); |
157 | 149 | } |
158 | 150 | 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 | + } |
160 | 162 | } |
161 | 163 | } |
162 | 164 |
|
@@ -286,4 +288,26 @@ public void syntaxError( |
286 | 288 | } |
287 | 289 | } |
288 | 290 | } |
| 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 | + } |
289 | 313 | } |
0 commit comments