【发布时间】:2021-05-22 12:44:51
【问题描述】:
我正在使用典型的 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 端点提供所有配置文件,但它应该只提供 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: 使用在 MongoRepositoriesRegistrar.EnableMongoRepositoriesConfiguration 上声明的 @EnableMongoRepositories 中定义的 com.example.profile.repository.sub1profile.Sub1ProfileRepository 中定义的名称“sub1ProfileRepository”创建 bean 时出错:调用 init 方法失败;嵌套异常是 org.springframework.data.mapping.PropertyReferenceException: No property custom found for type Sub1Profile!
我错过了什么?
【问题讨论】:
-
你在连接差异数据库吗?
-
@zatef 不,它是一个只有一个集合的数据库。所有配置文件都应存储在这个单一集合中。
标签: java spring spring-data-mongodb querydsl