Skip to content

Commit dd7aa94

Browse files
committed
fix for Bean Validation annotations on @query method parameters
Signed-off-by: Gavin King <[email protected]>
1 parent f6add9d commit dd7aa94

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

tooling/metamodel-generator/src/jakartaData/java/org/hibernate/processor/test/data/eg/Bookshop.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import jakarta.data.repository.Query;
66
import jakarta.data.repository.Repository;
77
import jakarta.transaction.Transactional;
8+
import jakarta.validation.constraints.NotBlank;
9+
import jakarta.validation.constraints.NotNull;
810

911
import java.util.List;
1012

@@ -15,10 +17,10 @@ public interface Bookshop extends CrudRepository<Book,String> {
1517
List<Book> byPublisher(String publisher_name);
1618

1719
@Query("select isbn where title like ?1 order by isbn")
18-
String[] ssns(String title);
20+
String[] ssns(@NotBlank String title);
1921

2022
@Query("select count(this) where title like ?1 order by isbn")
21-
long count1(String title);
23+
long count1(@NotNull String title);
2224

2325
@Query("select count(this) where this.title like ?1 order by this.isbn")
2426
long count2(String title);

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2562,6 +2562,16 @@ private void checkOrdinalParameter(
25622562
}
25632563
}
25642564

2565+
private static String stripTypeAnnotations(String argType) {
2566+
while ( argType.startsWith("@") ) {
2567+
int index = argType.indexOf(' ');
2568+
if (index>0) {
2569+
argType = argType.substring(index+1);
2570+
}
2571+
}
2572+
return argType;
2573+
}
2574+
25652575
private void checkNamedParameter(
25662576
SqmParameter<?> param, List<String> paramNames, List<String> paramTypes, ExecutableElement method,
25672577
AnnotationMirror mirror, AnnotationValue value, String queryParamType) {
@@ -2584,7 +2594,8 @@ private void checkNamedParameter(
25842594
}
25852595
}
25862596

2587-
private static boolean isLegalAssignment(SqmParameter<?> param, String argType, String queryParamType) {
2597+
private static boolean isLegalAssignment(SqmParameter<?> param, String argumentType, String queryParamType) {
2598+
final String argType = stripTypeAnnotations(argumentType);
25882599
return param.allowMultiValuedBinding()
25892600
? isLegalAssignment(argType, LIST + '<' + queryParamType + '>')
25902601
: isLegalAssignment(argType, queryParamType);
@@ -2636,7 +2647,12 @@ private List<String> parameterTypes(ExecutableElement method) {
26362647
private String typeAsString(TypeMirror type) {
26372648
String result = type.toString();
26382649
for ( AnnotationMirror annotation : type.getAnnotationMirrors() ) {
2639-
result = result.replace(annotation.toString(), "");
2650+
final String annotationString = annotation.toString();
2651+
result = result
2652+
// if it has a space after it, we need to remove that too
2653+
.replace(annotationString + ' ', "")
2654+
// just in case it did not have a space after it
2655+
.replace(annotationString, "");
26402656
}
26412657
for ( AnnotationMirror annotation : type.getAnnotationMirrors() ) {
26422658
result = annotation.toString() + ' ' + result;

0 commit comments

Comments
 (0)