Spring Data MongoDB 存储库中的 @Aggregation 注解问题

问题描述 投票:0回答:2

我在 Spring Boot 应用程序的 Spring Data MongoDB 存储库中遇到 @Aggregation 注释的问题。虽然 @Query 注释按预期工作,但使用 @Aggregation 会导致异常,这表明在处理 @Aggregation 注释时存在潜在的错误或不当行为。

环境

Spring Boot版本:3.1.5 Spring Data MongoDB 版本:3.1.5 MongoDB 版本:6.0

代码示例

我有一个自定义存储库接口,使用 @Query 和 @Aggregation 注释的方法扩展 CrudRepository。这是代码的相关部分:

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();
}

遇到异常

在应用程序启动时创建存储库时,应用程序抛出以下异常:

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

这是异常之前的日志(我设置了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…

其他背景

  • @Query 注解方法(myOtherCustomAccount)工作没有任何问题,表明 MongoDB 存储库的基本设置和配置是正确的。
  • 此问题仅出现在 @Aggregation 注释方法 (myCustomAggregation) 中。
  • 我已确认我的应用程序配置和依赖项已为 Spring Data MongoDB 正确设置,并且 Spring 已正确扫描存储库。
  • 我还验证了我使用的是支持 @Aggregation 注释的 Spring Data MongoDB 的兼容版本。

鉴于 @Query 注释工作正常,并且该问题似乎与 @Aggregation 无关,我怀疑这可能是 Spring Data MongoDB 处理 @Aggregation 注释时的错误或未记录的行为。

任何解决此问题的见解或帮助将不胜感激。

感谢您的时间和帮助。

mongodb spring-boot repository spring-data-mongodb spring-annotations
2个回答
1
投票

根本原因是我的 Spring Data MongoDB 配置中的自定义repositoryFactoryBeanClass 设置。此自定义策略重新定义了 QueryLookupStrategy,干扰了存储库注释的正常处理,包括 @Aggregation。


0
投票

遇到同样的问题。您能否简要描述一下您为解决此问题所做的所有更改?

© www.soinside.com 2019 - 2024. All rights reserved.