fix for Bean Validation annotations on @Query method parameters

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-03-30 15:24:33 +01:00
parent f6add9dbbb
commit dd7aa947c1
2 changed files with 22 additions and 4 deletions

View File

@ -5,6 +5,8 @@ import jakarta.data.repository.Find;
import jakarta.data.repository.Query; import jakarta.data.repository.Query;
import jakarta.data.repository.Repository; import jakarta.data.repository.Repository;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.util.List; import java.util.List;
@ -15,10 +17,10 @@ public interface Bookshop extends CrudRepository<Book,String> {
List<Book> byPublisher(String publisher_name); List<Book> byPublisher(String publisher_name);
@Query("select isbn where title like ?1 order by isbn") @Query("select isbn where title like ?1 order by isbn")
String[] ssns(String title); String[] ssns(@NotBlank String title);
@Query("select count(this) where title like ?1 order by isbn") @Query("select count(this) where title like ?1 order by isbn")
long count1(String title); long count1(@NotNull String title);
@Query("select count(this) where this.title like ?1 order by this.isbn") @Query("select count(this) where this.title like ?1 order by this.isbn")
long count2(String title); long count2(String title);

View File

@ -2562,6 +2562,16 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
} }
private static String stripTypeAnnotations(String argType) {
while ( argType.startsWith("@") ) {
int index = argType.indexOf(' ');
if (index>0) {
argType = argType.substring(index+1);
}
}
return argType;
}
private void checkNamedParameter( private void checkNamedParameter(
SqmParameter<?> param, List<String> paramNames, List<String> paramTypes, ExecutableElement method, SqmParameter<?> param, List<String> paramNames, List<String> paramTypes, ExecutableElement method,
AnnotationMirror mirror, AnnotationValue value, String queryParamType) { AnnotationMirror mirror, AnnotationValue value, String queryParamType) {
@ -2584,7 +2594,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
} }
private static boolean isLegalAssignment(SqmParameter<?> param, String argType, String queryParamType) { private static boolean isLegalAssignment(SqmParameter<?> param, String argumentType, String queryParamType) {
final String argType = stripTypeAnnotations(argumentType);
return param.allowMultiValuedBinding() return param.allowMultiValuedBinding()
? isLegalAssignment(argType, LIST + '<' + queryParamType + '>') ? isLegalAssignment(argType, LIST + '<' + queryParamType + '>')
: isLegalAssignment(argType, queryParamType); : isLegalAssignment(argType, queryParamType);
@ -2636,7 +2647,12 @@ public class AnnotationMetaEntity extends AnnotationMeta {
private String typeAsString(TypeMirror type) { private String typeAsString(TypeMirror type) {
String result = type.toString(); String result = type.toString();
for ( AnnotationMirror annotation : type.getAnnotationMirrors() ) { for ( AnnotationMirror annotation : type.getAnnotationMirrors() ) {
result = result.replace(annotation.toString(), ""); final String annotationString = annotation.toString();
result = result
// if it has a space after it, we need to remove that too
.replace(annotationString + ' ', "")
// just in case it did not have a space after it
.replace(annotationString, "");
} }
for ( AnnotationMirror annotation : type.getAnnotationMirrors() ) { for ( AnnotationMirror annotation : type.getAnnotationMirrors() ) {
result = annotation.toString() + ' ' + result; result = annotation.toString() + ' ' + result;