Issue with @Aggregation Annotation in Spring Data MongoDB Repository

88 Views Asked by At

I am encountering an issue with the @Aggregation annotation in a Spring Data MongoDB repository in my Spring Boot application. While the @Query annotation works as expected, using @Aggregation results in an exception, suggesting a potential bug or misbehavior in handling the @Aggregation annotation.

Environment

Spring Boot version: 3.1.5 Spring Data MongoDB version: 3.1.5 MongoDB version: 6.0

Code Sample

I have a custom repository interface extending CrudRepository with methods using both @Query and @Aggregation annotations. Here's the relevant part of the code:

import com.myproject.resource.AccountEntity;
import org.springframework.data.mongodb.repository.Aggregation;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MyCustomRepository extends CrudRepository<AccountEntity, String> {
    @Query(value = "{ '_id': ?0 }")
    AccountEntity myOtherCustomAccount(String id );

    @Aggregation("{ '$project': { '_id' : '$lasName' } }")
    List<String> myCustomAggregation();
}

Exception Encountered

At application startup when creating creating the repository, the application throws the following exception:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property 'myCustomAggregation' found for type 'AccountEntity'

Here is the log right before the exception (I set logging.level.org.springframework.data=DEBUG)

2023-11-28T14:03:19.910+01:00 DEBUG 38160 --- [  restartedMain] o.s.d.r.c.s.RepositoryFactorySupport     : Initializing repository instance for com.sword.signature.model.repository.resource.MyCustomRepository…

Additional Context

  • The @Query annotated method (myOtherCustomAccount) works without any issues, indicating that the basic setup and configuration of the MongoDB repository are correct.
  • The issue only arises with the @Aggregation annotated method (myCustomAggregation).
  • I have confirmed that my application configuration and dependencies are correctly set up for Spring Data MongoDB, and the repository is properly scanned by Spring.
  • I have also verified that I'm using a compatible version of Spring Data MongoDB that supports the @Aggregation annotation.

Given that the @Query annotation works correctly, and the issue seems isolated to @Aggregation, I suspect this might be a bug or an undocumented behavior in Spring Data MongoDB's handling of the @Aggregation annotation.

Any insights or assistance in resolving this issue would be greatly appreciated.

Thank you for your time and help.

1

There are 1 best solutions below

0
Richard N On

The root cause was a custom repositoryFactoryBeanClass setting in my Spring Data MongoDB configuration. This custom strategy was redefining the QueryLookupStrategy interfering with the normal processing of repository annotations, including @Aggregation.