From 14151fdb97b15561f20dcb3e62defce8c6b53bcd Mon Sep 17 00:00:00 2001 From: Gavin King Date: Tue, 5 Sep 2023 18:36:15 +0200 Subject: [PATCH] improvements to validation of @HQL method return type --- .../annotation/AnnotationMetaEntity.java | 14 +++++++++++--- .../hibernate/jpamodelgen/test/dao/Dao.java | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java index 8589115ea5..0bb292d939 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java @@ -441,11 +441,11 @@ public class AnnotationMetaEntity extends AnnotationMeta { private void addQueryMethod(ExecutableElement method) { final TypeMirror returnType = method.getReturnType(); final TypeKind kind = returnType.getKind(); - if ( kind == TypeKind.VOID || kind.isPrimitive() ) { + if ( kind == TypeKind.VOID || kind == TypeKind.ARRAY || kind.isPrimitive() ) { addQueryMethod( method, returnType, null ); } else if ( kind == TypeKind.DECLARED ) { - final DeclaredType declaredType = ununi((DeclaredType) returnType); + final DeclaredType declaredType = ununi( (DeclaredType) returnType ); final TypeElement typeElement = (TypeElement) declaredType.asElement(); final List typeArguments = declaredType.getTypeArguments(); switch ( typeArguments.size() ) { @@ -1060,7 +1060,15 @@ public class AnnotationMetaEntity extends AnnotationMeta { } else { // TODO: anything more we can do here? e.g. check constructor - returnTypeCorrect = true; + try { + final Class javaResultType = selection.getJavaType(); + final TypeElement typeElement = context.getTypeElementForFullyQualifiedName( javaResultType.getName() ); + returnTypeCorrect = context.getTypeUtils().isAssignable( returnType, typeElement.asType() ); + } + catch (Exception e) { + //ignore + returnTypeCorrect = true; + } } if ( !returnTypeCorrect ) { context.message(method, mirror, value, diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/dao/Dao.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/dao/Dao.java index 50fd3955b6..624bb81e37 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/dao/Dao.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/dao/Dao.java @@ -63,6 +63,24 @@ public interface Dao { // @HQL("from Book where title like :title") // SelectionQuery findByTitleWithOrderingByVarargs(String title, Order... order); + @HQL("select count(*) from Book") + long countBooks(); + + @HQL("select count(*)>1 from Book") + boolean booksExist(); + + @HQL("delete from Book") + int deleteBooks(); + + @HQL("select count(*), count(*)>1 from Book") + Object[] funnyQueryReturningArray(); + + class Record { + Record(Long count, Boolean exists) {} + } + @HQL("select count(*), count(*)>1 from Book") + Record funnyQueryReturningRecord(); + @HQL("from Book where isbn = :isbn") Book findByIsbn(String isbn);