diff --git a/tooling/metamodel-generator/src/jakartaData/java/org/hibernate/processor/test/data/BookAuthorRepository.java b/tooling/metamodel-generator/src/jakartaData/java/org/hibernate/processor/test/data/BookAuthorRepository.java index ffd96d14c3..564c2d0c4d 100644 --- a/tooling/metamodel-generator/src/jakartaData/java/org/hibernate/processor/test/data/BookAuthorRepository.java +++ b/tooling/metamodel-generator/src/jakartaData/java/org/hibernate/processor/test/data/BookAuthorRepository.java @@ -48,6 +48,9 @@ public interface BookAuthorRepository { @Find Book book(String isbn); + @Find + Optional bookMaybe(@By("#id") String id); + @Find Book[] books(@By("isbn") String[] isbns); diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java index 4260e5a4d9..10e5e01b9d 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java @@ -83,6 +83,7 @@ import static org.hibernate.processor.util.TypeUtils.getAnnotationValue; import static org.hibernate.processor.util.TypeUtils.getAnnotationValueRef; import static org.hibernate.processor.util.TypeUtils.hasAnnotation; import static org.hibernate.processor.util.TypeUtils.primitiveClassMatchesKind; +import static org.hibernate.processor.util.TypeUtils.propertyName; /** * Class used to collect meta information about an annotated type (entity, embeddable or mapped superclass). @@ -1007,7 +1008,7 @@ public class AnnotationMetaEntity extends AnnotationMeta { private void createCriteriaFinder( ExecutableElement method, TypeMirror returnType, @Nullable String containerType, TypeElement entity) { final String methodName = method.getSimpleName().toString(); - final List paramNames = parameterNames( method ); + final List paramNames = parameterNames( method, entity ); final List paramTypes = parameterTypes( method ); final String[] sessionType = sessionTypeFromParameters( paramNames, paramTypes ); final String methodKey = methodName + paramTypes; @@ -1206,7 +1207,7 @@ public class AnnotationMetaEntity extends AnnotationMeta { private void createMultipleParameterFinder(ExecutableElement method, TypeMirror returnType, TypeElement entity) { final String methodName = method.getSimpleName().toString(); - final List paramNames = parameterNames( method ); + final List paramNames = parameterNames( method, entity ); final List paramTypes = parameterTypes( method ); final String[] sessionType = sessionTypeFromParameters( paramNames, paramTypes ); final String methodKey = methodName + paramTypes; @@ -1270,7 +1271,7 @@ public class AnnotationMetaEntity extends AnnotationMeta { method.getParameters().stream() .filter(AnnotationMetaEntity::isFinderParameterMappingToAttribute) .findFirst().orElseThrow(); - final List paramNames = parameterNames( method ); + final List paramNames = parameterNames( method, entity ); final List paramTypes = parameterTypes( method ); final String[] sessionType = sessionTypeFromParameters( paramNames, paramTypes ); final FieldType fieldType = validateFinderParameter( entity, parameter ); @@ -1520,8 +1521,11 @@ public class AnnotationMetaEntity extends AnnotationMeta { final AccessType accessType = getAccessType(entityType); final String nextToken = tokens.nextToken(); for ( Element member : entityType.getEnclosedElements() ) { + if ( "#id".equals(nextToken) && hasAnnotation( member, ID) ) { + return member; + } final Element match = - memberMatchingPath(entityType, member, accessType, tokens, nextToken); + memberMatchingPath( entityType, member, accessType, tokens, nextToken ); if ( match != null ) { return match; } @@ -2038,6 +2042,20 @@ public class AnnotationMetaEntity extends AnnotationMeta { .collect(toList()); } + private List parameterNames(ExecutableElement method, TypeElement entity) { + final String idName = + // account for special @By("#id") hack in Jakarta Data + entity.getEnclosedElements().stream() + .filter(member -> hasAnnotation(member, ID)) + .map(member -> propertyName(this, member)) + .findFirst() + .orElse("id"); + return method.getParameters().stream() + .map(AnnotationMetaEntity::parameterName) + .map(name -> "#id".equals(name) ? idName : name) + .collect(toList()); + } + private static List parameterNames(ExecutableElement method) { return method.getParameters().stream() .map(AnnotationMetaEntity::parameterName)