简体   繁体   English

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

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

I am building a profile service with the typical REST endpoints for creating, reading, updating and deleting profiles.我正在使用典型的 REST 端点构建配置文件服务,用于创建、读取、更新和删除配置文件。 For this I am using the Spring Framework together with a MongoDB.为此,我将 Spring 框架与 MongoDB 一起使用。 On top I would like to use QueryDSL to create some custom queries.最重要的是,我想使用 QueryDSL 创建一些自定义查询。

A full minimal working example of the current implementation can be found here: https://github.com/mirrom/profile-modules可以在此处找到当前实现的完整最小工作示例: https://github.com/mirrom/profile-modules

I would like to have sub profile models that extend the base profile model, and sub sub models that extend the sub models.我想要扩展基本配置文件 model 的子配置文件模型,以及扩展子模型的子子模型。 By this I have hierarchical profiles that inherit the fields of its parent profile.通过这个,我有继承其父配置文件字段的分层配置文件。 The idea is to store all profiles in the same collection and distinguish them via the automatically created _class field.这个想法是将所有配置文件存储在同一个集合中,并通过自动创建的_class字段区分它们。

A simple example (with Lombok annotations):一个简单的例子(带有 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;
    
}

While (all) profiles can get accessed via the endpoint /api/v1/profiles , the sub1Profiles can be accessed via /api/v1/profiles/sub-1-profiles .虽然(所有)配置文件可以通过端点/api/v1/profiles访问,但 sub1Profiles 可以通过/api/v1/profiles/sub-1-profiles访问。 Currently the sub1Profiles endpoint delivers all profiles, but it should just deliver the sub1Profiles and its children.目前 sub1Profiles 端点提供所有配置文件,但它应该只提供 sub1Profiles 及其子项。 For this I would like to use QueryDSL, but I can't add QuerydslPredicateExecutor<Profile> and QuerydslBinderCustomizer<QProfile> to more than one repository interface.为此,我想使用 QueryDSL,但我不能将QuerydslPredicateExecutor<Profile>QuerydslBinderCustomizer<QProfile>添加到多个存储库接口。 This is how my profile repository looks like:这是我的配置文件存储库的样子:

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

If I now try to do the same with Sub1ProfileRepository:如果我现在尝试对 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);
    }
    
}

I get this error message:我收到此错误消息:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sub1ProfileRepository' defined in com.example.profile.repository.sub1profile.Sub1ProfileRepository defined in @EnableMongoRepositories declared on MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration: Invocation of init method failed; org.springframework.beans.factory.BeanCreationException:创建名为“sub1ProfileRepository”的bean时出错,在com.example.profile.repository.sub1profile.Sub1ProfileRepository中定义在MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration上声明的@EnableMongoRepositories中定义:调用init方法失败; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property customize found for type Sub1Profile!嵌套异常是 org.springframework.data.mapping.PropertyReferenceException: No property custom found for type Sub1Profile!

What am I missing?我错过了什么?

In Sub1ProfileRepository 's customize method, you have used QProfile as method argument.Sub1ProfileRepositorycustomize方法中,您使用QProfile作为方法参数。 Can you use QSub1Profile instead and check if it's working?您可以改用QSub1Profile并检查它是否有效吗?

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

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