簡體   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