QueryDSL isNotEmpty表达式失败

时间:2015-03-17 16:12:19

标签: spring-data-mongodb querydsl

我尝试执行一个表达式,该表达式返回一组结果,其中collection属性不为空。这是我的课程

的A.class

@Document
public class A {
  @Id String id;
  String name;
  List<B> matches;
}

B.class

@Document
public class B {
    String name;

}

我的测试用例

@Test
public void testFindWhereCollectionNotEmpty() {

  B b1 = new B();
  b1.name = "b1";

  B b2 = new B();
  b2.name = "b2";

  template.save(b1);
  template.save(b2);

  A a1 = new A();
  a1.id = "p1";
  a1.matches = Arrays.asList(b1, b2);

  A a2 = new A();
  a2.id = "a2";
  a2.matches = new ArrayList<B>();

  A a3 = new A();
  a3.id = "a3";
  a3.matches = null;

  template.save(a1);
  template.save(a2);
  template.save(a3);

  QA qa = QA.a;


  BooleanExpression expr = qa.matches.isNotEmpty();
  Iterable<A> result = aRepo.findAll(expr);

  assertThat(result, is(not((emptyIterable()))));
}

当我执行此测试时,我收到以下错误:

com.mongodb.MongoException: Can't canonicalize query: BadValue $or needs an array
    at com.mongodb.QueryResultIterator.throwOnQueryFailure(QueryResultIterator.java:214)
    at com.mongodb.QueryResultIterator.init(QueryResultIterator.java:198)
    at com.mongodb.QueryResultIterator.initFromQueryResponse(QueryResultIterator.java:176)
    at com.mongodb.QueryResultIterator.<init>(QueryResultIterator.java:64)
    at com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:86)
    at com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:66)
    at com.mongodb.DBCursor._check(DBCursor.java:458)
    at com.mongodb.DBCursor._hasNext(DBCursor.java:546)
    at com.mongodb.DBCursor.hasNext(DBCursor.java:571)
    at com.mysema.query.mongodb.MongodbQuery.list(MongodbQuery.java:253)
    at org.springframework.data.mongodb.repository.support.QueryDslMongoRepository.findAll(QueryDslMongoRepository.java:102)

您可以在https://github.com/tedp/spring-querydsl-test

为自己试一试

我做错了吗?

1 个答案:

答案 0 :(得分:1)

此问题现已在QueryDSL的3.6.3版中得到修复。有关详细信息,请参阅https://github.com/querydsl/querydsl/issues/1264

非常感谢所有参与解决方案的人!