querydsl无法将属性从类型long序列化为字符串

d7v8vwbk  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(597)

我正在尝试使用querydsl、mongodb和spring数据查询/过滤一些简单的数据。

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>2.2.0.M4</version>
</dependency>
<dependency>
        <groupId>com.querydsl</groupId>
        <version>4.2.1</version>
        <artifactId>querydsl-core</artifactId>
</dependency>
<dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-apt</artifactId>
        <version>4.2.1</version>
</dependency>
<dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-mongodb</artifactId>
        <version>4.2.1</version>
</dependency>
@Getter
@Setter
@Document
public class Library {

  private Address address;

  @NotNull
  private Long books = 4L;

  ...

}

我的第一个方法:

public Page<Library> findAll(Predicate predicate, MultiValueMap<String, String> parameters) {
    final QLibrary qLibrary = QLibrary.library;
    final BooleanBuilder builder = new BooleanBuilder();

    if (parameters.containsKey("booksCount")) {
      builder.and(qLibrary.booksCount.stringValue().startsWith(parameters.getFirst("booksCount")));
    }

    final BooleanExpression expression = (BooleanExpression) predicate;
    return repository.findAll(expression.and(builder.getValue()), Pageable.unpaged());
}

我的第二种方法:

public class LibraryBindings implements QuerydslBinderCustomizer<QLibrary> {

    @Override
    public void customize(QuerydslBindings bindings, QLibrary root) {
        bindings.bind(root.booksCount).first(new SingleValueBinding<NumberPath<Long>, Long>() {
            @Override
            public Predicate bind(NumberPath<Long> path, Long value) {
                return path.stringValue().startsWith(value.toString());
            }
        });
    }

}

我的第三种方法:

public class LibraryBindings implements QuerydslBinderCustomizer<QLibrary> {

    @Override
    public void customize(QuerydslBindings bindings, QLibrary root) {
        bindings.bind(root.booksCount).first(new SingleValueBinding<NumberPath<Long>, Long>() {
            @Override
            public Predicate bind(NumberPath<Long> path, Long value) {
                return path.like(value.toString());
            }
        });
    }

}

使用上述方法,我得到一个例外:

java.lang.UnsupportedOperationException: Illegal operation str(library.booksCount)
    at org.springframework.data.mongodb.repository.support.MongodbDocumentSerializer.visit(MongodbDocumentSerializer.java:323) ~[spring-data-mongodb-2.2.0.M4.jar:2.2.0.M4]
    at org.springframework.data.mongodb.repository.support.MongodbDocumentSerializer.visit(MongodbDocumentSerializer.java:54) ~[spring-data-mongodb-2.2.0.M4.jar:2.2.0.M4]
    at com.querydsl.core.types.OperationImpl.accept(OperationImpl.java:83) ~[querydsl-core-4.2.1.jar:na]
    at org.springframework.data.mongodb.repository.support.MongodbDocumentSerializer.asDBValue(MongodbDocumentSerializer.java:146) ~[spring-data-mongodb-2.2.0.M4.jar:2.2.0.M4]
    at org.springframework.data.mongodb.repository.support.MongodbDocumentSerializer.asDBKey(MongodbDocumentSerializer.java:138) ~[spring-data-mongodb-2.2.0.M4.jar:2.2.0.M4]
    at org.springframework.data.mongodb.repository.support.SpringDataMongodbSerializer.asDBKey(SpringDataMongodbSerializer.java:158) ~[spring-data-mongodb-2.2.0.M4.jar:2.2.0.M4]
    at org.springframework.data.mongodb.repository.support.MongodbDocumentSerializer.visit(MongodbDocumentSerializer.java:227) ~[spring-data-mongodb-2.2.0.M4.jar:2.2.0.M4]
    at org.springframework.data.mongodb.repository.support.MongodbDocumentSerializer.visit(MongodbDocumentSerializer.java:54) ~[spring-data-mongodb-2.2.0.M4.jar:2.2.0.M4]
    at com.querydsl.core.types.OperationImpl.accept(OperationImpl.java:83) ~[querydsl-core-4.2.1.jar:na]
    at org.springframework.data.mongodb.repository.support.MongodbDocumentSerializer.handle(MongodbDocumentSerializer.java:58) ~[spring-data-mongodb-2.2.0.M4.jar:2.2.0.M4]
    at org.springframework.data.mongodb.repository.support.MongodbDocumentSerializer.visit(MongodbDocumentSerializer.java:185) ~[spring-data-mongodb-2.2.0.M4.jar:2.2.0.M4]
    at org.springframework.data.mongodb.repository.support.MongodbDocumentSerializer.visit(MongodbDocumentSerializer.java:54) ~[spring-data-mongodb-2.2.0.M4.jar:2.2.0.M4]
    at com.querydsl.core.types.OperationImpl.accept(OperationImpl.java:83) ~[querydsl-core-4.2.1.jar:na]
    at org.springframework.data.mongodb.repository.support.MongodbDocumentSerializer.handle(MongodbDocumentSerializer.java:58) ~[spring-data-mongodb-2.2.0.M4.jar:2.2.0.M4]
    at org.springframework.data.mongodb.repository.support.MongodbDocumentSerializer.toQuery(MongodbDocumentSerializer.java:69) ~[spring-data-mongodb-2.2.0.M4.jar:2.2.0.M4]
    at org.springframework.data.mongodb.repository.support.QuerydslAbstractMongodbQuery.createQuery(QuerydslAbstractMongodbQuery.java:166) ~[spring-data-mongodb-2.2.0.M4.jar:2.2.0.M4]
    at org.springframework.data.mongodb.repository.support.QuerydslFetchableMongodbQuery.createQuery(QuerydslFetchableMongodbQuery.java:197) ~[spring-data-mongodb-2.2.0.M4.jar:2.2.0.M4]
    at org.springframework.data.mongodb.repository.support.QuerydslFetchableMongodbQuery.createQuery(QuerydslFetchableMongodbQuery.java:190) ~[spring-data-mongodb-2.2.0.M4.jar:2.2.0.M4]
    at org.springframework.data.mongodb.repository.support.QuerydslFetchableMongodbQuery.fetch(QuerydslFetchableMongodbQuery.java:111) ~[spring-data-mongodb-2.2.0.M4.jar:2.2.0.M4]
    at org.springframework.data.mongodb.repository.support.SpringDataMongodbQuery.fetch(SpringDataMongodbQuery.java:28) ~[spring-data-mongodb-2.2.0.M4.jar:2.2.0.M4]
    at org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor.findAll(QuerydslMongoPredicateExecutor.java:160) ~[spring-data-mongodb-2.2.0.M4.jar:2.2.0.M4]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
    at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359) ~[spring-data-commons-2.2.0.M4.jar:2.2.0.M4]
    at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200) ~[spring-data-commons-2.2.0.M4.jar:2.2.0.M4]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:641) ~[spring-data-commons-2.2.0.M4.jar:2.2.0.M4]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.0.M2.jar:5.2.0.M2]

所有其他过滤器/查询都正常工作(比较字符串、比较数字与数字或布尔值与布尔值等等)。querydsl配置正确。
假设用户可以通过 booksCount 他有四个可用的库对象:
带1本书
有7本书
有12本书
有127本书。
当用户按图书数量和类型数字x进行筛选时,我想显示数量为y的对象:
当用户键入1时,它应该显示具有 booksCount 1, 12, 127.
当用户键入12时,它应该显示带有 booksCount 12, 127.
当用户键入7时,它应该显示带有 booksCount 7.
当用户键入13时,它不应该显示任何对象。
当用户输入126时,它不应该显示任何对象。
当用户键入127时,它应该显示带有 booksCount 127
就这么简单。我的问题是:
为什么会发生此异常以及如何修复它?
如果这是一个bug(请不要),我如何用这个架构实现相同的行为?
先谢谢你。

ubby3x7f

ubby3x7f1#

据我们所知 DBObject 查询不支持将数字强制转换为字符串,因此 stringValue() 使用时当前不支持操作 querydsl-mongodb .

相关问题