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

问题描述

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

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

我想要扩展基本配置文件模型的子配置文件模型,以及扩展子模型的子子模型。通过这个,我有继承其父配置文件字段的分层配置文件。这个想法是将所有配置文件存储在同一个集合中,并通过自动创建的 _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,QuerydslPredicateExecutor<Sub1Profile>,QuerydslBinderCustomizer<QSub1Profile> {
    
    default void customize(QuerydslBindings bindings,String>) StringExpression::containsIgnoreCase);
    }
    
}

我收到此错误消息:

org.springframework.beans.factory.BeanCreationException:在 MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration 上声明的 @EnableMongoRepositories 中定义的 com.example.profile.repository.sub1profile.Sub1ProfileRepository 中定义的名称为“sub1ProfileRepository”的 bean 创建时出错:调用 init 方法失败;嵌套异常是 org.springframework.data.mapping.PropertyReferenceException:未找到类型 Sub1Profile 的属性自定义

我错过了什么?

解决方法

#define MaxSize 1024 #define ElementType int struct DStack { ElementType Data[MaxSize]; int Top1; // Stack top 1 int Top2; // the top 2 } S,S2; int main(void) { S.Top1 = -1; S.Top2 = MaxSize; } Sub1ProfileRepository 方法中,您使用了 customize 作为方法参数。您可以改用 QProfile 并检查它是否有效吗?

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...