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

5vf7fwbs  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(333)

我正在用典型的rest端点构建一个概要文件服务,用于创建、读取、更新和删除概要文件。为此,我将spring框架与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 ,可以通过 /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时出错,该bean在com.example.profile.repository.sub1profile.sub1profilerepository中定义,该bean在mongorepositoriesregistrar.enablemongorepositoriesconfiguration上声明@enablemongorepositories中:调用init方法失败;嵌套异常为org.springframework.data.mapping.propertyreferenceexception:找不到类型sub1profile的属性自定义!
我错过了什么?

t5fffqht

t5fffqht1#

Sub1ProfileRepositorycustomize 方法,你已经用过了 QProfile 作为方法参数。你能用吗 QSub1Profile 而是检查它是否工作?

相关问题