繁体   English   中英

如何在多个 mongo 存储库上使用 QueryDSL?

[英]How can I use QueryDSL on multiple mongo repositories?

我正在使用典型的 REST 端点构建配置文件服务,用于创建、读取、更新和删除配置文件。 为此,我将 Spring 框架与 MongoDB 一起使用。 最重要的是,我想使用 QueryDSL 创建一些自定义查询。

可以在此处找到当前实现的完整最小工作示例: https://github.com/mirrom/profile-modules

我想要扩展基本配置文件 model 的子配置文件模型,以及扩展子模型的子子模型。 通过这个,我有继承其父配置文件字段的分层配置文件。 这个想法是将所有配置文件存储在同一个集合中,并通过自动创建的_class字段区分它们。

一个简单的例子(带有 Lombok 注释):

@Data
@Document(collection = "profiles")
@Entity
public class Profile {
    
    @Id
    private ObjectId id;
    
    @Indexed
    private String title;
    
    @Indexed
    private String description;
    
    private LocalDateTime createdAt;
    
    private LocalDateTime modifiedAt;
    
}
@Data
@Entity
@EqualsAndHashCode(callSuper = true)
public class Sub1Profile extends Profile {
    
    private String sub1String;
    
    private int sub1Integer;
    
}

虽然(所有)配置文件可以通过端点/api/v1/profiles访问,但 sub1Profiles 可以通过/api/v1/profiles/sub-1-profiles访问。 目前 sub1Profiles 端点提供所有配置文件,但它应该只提供 sub1Profiles 及其子项。 为此,我想使用 QueryDSL,但我不能将QuerydslPredicateExecutor<Profile>QuerydslBinderCustomizer<QProfile>添加到多个存储库接口。 这是我的配置文件存储库的样子:

@Repository
public interface ProfileRepository extends MongoRepository<Profile, ObjectId>, QuerydslPredicateExecutor<Profile>,
        QuerydslBinderCustomizer<QProfile> {
    
    @Override
    default void customize(QuerydslBindings bindings, QProfile root) {
        
        bindings.bind(String.class)
                .first((SingleValueBinding<StringPath, String>) StringExpression::containsIgnoreCase);
    }
    
}

如果我现在尝试对 Sub1ProfileRepository 做同样的事情:

@Repository
public interface Sub1ProfileRepository
        extends MongoRepository<Sub1Profile, ObjectId>, QuerydslPredicateExecutor<Sub1Profile>,
        QuerydslBinderCustomizer<QSub1Profile> {
    
    default void customize(QuerydslBindings bindings, QProfile root) {
        
        bindings.bind(String.class)
                .first((SingleValueBinding<StringPath, String>) StringExpression::containsIgnoreCase);
    }
    
}

我收到此错误消息:

org.springframework.beans.factory.BeanCreationException:创建名为“sub1ProfileRepository”的bean时出错,在com.example.profile.repository.sub1profile.Sub1ProfileRepository中定义在MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration上声明的@EnableMongoRepositories中定义:调用init方法失败; 嵌套异常是 org.springframework.data.mapping.PropertyReferenceException: No property custom found for type Sub1Profile!

我错过了什么?

Sub1ProfileRepositorycustomize方法中,您使用QProfile作为方法参数。 您可以改用QSub1Profile并检查它是否有效吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM