"Is there any short hand to no .of rows in a database against a where condition" if we have a criteria definition present against which want number of rows present in database. is is any function in r2dbc that give that?
Asked
Active
Viewed 1,802 times
3
-
As it is about executing SQL, what is wrong with doing a `SELECT COUNT(*) FROM YOUR_TABLE WHERE
` why would that be different here? – M. Deinum Jul 07 '20 at 12:25 -
The condition that gets build up is dynamic, it depends on fields that are populated in my object of criteria definition – DEVYANI SETHI Jul 07 '20 at 12:49
-
1So? What prevents you from executing a dynamic query? If you can execute a query you can also execute the count query. – M. Deinum Jul 07 '20 at 12:55
-
How can we create a query from CriteriaObject the method execute() requires a string – DEVYANI SETHI Jul 08 '20 at 14:43
-
like we have select(), insert() methods in r2dbc so do we have a method for count as well – DEVYANI SETHI Jul 08 '20 at 14:44
-
what do you think `select` does. How is a `select count(*)` different from `select foo, bar from baz`. It is just a select statement. – M. Deinum Jul 08 '20 at 14:47
-
@DEVYANISETHI It's been quite a while since you asked this question. Any updates from your side? Did you try out the answer I had provided? – Abhinaba Chakraborty Jul 22 '20 at 16:35
1 Answers
2
You can have something like this:
@Repository
public class MyCustomRepositoryImpl {
private final R2dbcEntityTemplate r2dbcEntityTemplate;
@Autowired
public MyCustomRepositoryImpl(DatabaseClient databaseClient) {
this.r2dbcEntityTemplate = new R2dbcEntityTemplate(databaseClient);
}
public Mono<Long> getCountOfAllRows() {
return r2dbcEntityTemplate.count(query(Criteria.empty()),MyEntity.class);
}
public Mono<Long> getCountOfRowsForACondition(String myVal) {
return r2dbcEntityTemplate.select(query(Criteria.where("mycolumn").is(myVal)),MyEntity.class);
}
}
Note here query
is the static method of org.springframework.data.relational.core.query.Query

Abhinaba Chakraborty
- 3,488
- 2
- 16
- 37