简体   繁体   English

QueryDsl:选择符合条件的行数

[英]QueryDsl: Select count of rows that meets the criteria

I have 2 records in db. 我在db中有2条记录。 Where I will do this: 我将在哪里执行此操作:

QLoginRegistryEntry loginregistryentry = QLoginRegistryEntry.loginRegistryEntry;
JPAQuery query = new JPAQuery(JPA.em());
List<LoginRegistryEntry> result;
result = query.from(loginregistryentry)
              .where(loginregistryentry.inDate.startsWith(yearAndMonth))
              .list(loginregistryentry);

System.out.println(result.size());

It return 2, which is correct 返回2,这是正确的

When I do this: 当我这样做时:

QLoginRegistryEntry loginregistryentry2 = new QLoginRegistryEntry("a");
long count = query.from(loginregistryentry2).count();
System.out.println(count);

It return 4, which is wrong 返回4,这是错误的

What Am I doing wrong? 我究竟做错了什么? How should I download the count of rows that meets my criteria? 如何下载符合条件的行数?

I don't think you executed the queries in isolation. 我不认为您是孤立执行查询的。 Probably you just reused the query object from your first query and continued, you got a cross join and got 2 * 2 = 4 results. 可能您只是重用了第一个查询中的查询对象,然后继续,您得到了一个交叉联接,并获得了2 * 2 = 4个结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM