fix to @OrderBy for @Query methods with primary entity type

don't think this is even strictly-speaking required by Jakarta Data
but we will allow it

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-03-31 20:20:08 +02:00 committed by Christian Beikov
parent dcfc254635
commit 7d80b8a3db
1 changed files with 31 additions and 16 deletions

View File

@ -1541,23 +1541,26 @@ public class AnnotationMetaEntity extends AnnotationMeta {
Diagnostic.Kind.ERROR ); Diagnostic.Kind.ERROR );
} }
private List<OrderBy> orderByList(ExecutableElement method, TypeElement entityType) { private List<OrderBy> orderByList(ExecutableElement method, TypeElement returnType) {
final AnnotationMirror orderByList = final TypeElement entityType = implicitEntityType( returnType );
getAnnotationMirror( method, "jakarta.data.repository.OrderBy.List" ); if ( entityType != null ) {
if ( orderByList != null ) { final AnnotationMirror orderByList =
final List<OrderBy> result = new ArrayList<>(); getAnnotationMirror( method, "jakarta.data.repository.OrderBy.List" );
@SuppressWarnings("unchecked") if ( orderByList != null ) {
final List<AnnotationValue> list = (List<AnnotationValue>) final List<OrderBy> result = new ArrayList<>();
castNonNull( getAnnotationValue( orderByList, "value" ) ).getValue(); @SuppressWarnings("unchecked")
for ( AnnotationValue element : list ) { final List<AnnotationValue> list = (List<AnnotationValue>)
result.add( orderByExpression( castNonNull( (AnnotationMirror) element.getValue() ), entityType, method ) ); castNonNull( getAnnotationValue( orderByList, "value" ) ).getValue();
for ( AnnotationValue element : list ) {
result.add( orderByExpression( castNonNull( (AnnotationMirror) element.getValue() ), entityType, method ) );
}
return result;
}
final AnnotationMirror orderBy =
getAnnotationMirror( method, "jakarta.data.repository.OrderBy" );
if ( orderBy != null ) {
return List.of( orderByExpression(orderBy, entityType, method) );
} }
return result;
}
final AnnotationMirror orderBy =
getAnnotationMirror( method, "jakarta.data.repository.OrderBy" );
if ( orderBy != null ) {
return List.of( orderByExpression(orderBy, entityType, method) );
} }
return emptyList(); return emptyList();
} }
@ -2214,6 +2217,18 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
} }
private @Nullable TypeElement implicitEntityType(@Nullable TypeElement resultType) {
if ( resultType != null && hasAnnotation(resultType, ENTITY) ) {
return resultType;
}
else if ( primaryEntity != null ) {
return primaryEntity;
}
else {
return null;
}
}
private static String entityName(DeclaredType resultType, AnnotationMirror annotation) { private static String entityName(DeclaredType resultType, AnnotationMirror annotation) {
final AnnotationValue name = getAnnotationValue(annotation, "name"); final AnnotationValue name = getAnnotationValue(annotation, "name");
if (name != null) { if (name != null) {