如何在多个 mongo 存储库上使用 QueryDSL?
How can I use QueryDSL on multiple mongo repositories?
我正在使用典型的 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: 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; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property customize found for type Sub1Profile!
我错过了什么?
在 Sub1ProfileRepository
的 customize
方法中,您使用了 QProfile
作为方法参数。您可以改用 QSub1Profile
并检查它是否正常工作吗?
我正在使用典型的 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: 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; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property customize found for type Sub1Profile!
我错过了什么?
在 Sub1ProfileRepository
的 customize
方法中,您使用了 QProfile
作为方法参数。您可以改用 QSub1Profile
并检查它是否正常工作吗?