|
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; |
|
31 | 33 | import static org.hibernate.processor.util.TypeUtils.containsAnnotation; |
32 | 34 | import static org.hibernate.processor.util.TypeUtils.getAnnotationMirror; |
33 | 35 | import static org.hibernate.processor.util.TypeUtils.getAnnotationValue; |
| 36 | +import static org.hibernate.processor.validation.ProcessorSessionFactory.findEntityByUnqualifiedName; |
34 | 37 |
|
35 | 38 | public abstract class AnnotationMeta implements Metamodel { |
36 | 39 |
|
@@ -136,24 +139,23 @@ && getAnnotationValue( mirror, "resultClass" ) == null ) { |
136 | 139 | } |
137 | 140 | } |
138 | 141 |
|
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(); |
154 | 146 | } |
155 | 147 | 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 | + } |
157 | 159 | } |
158 | 160 | } |
159 | 161 |
|
@@ -283,4 +285,26 @@ public void syntaxError( |
283 | 285 | } |
284 | 286 | } |
285 | 287 | } |
| 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 | + } |
286 | 310 | } |
0 commit comments