Description
I have many complex native queries where Spring Data can't figure out count queries out of the box. Think of anything involving SELECT DISTINCT
. My queries return correct results, but the total count is incorrect when using pagination. The total result is 300 records, but the count returns 650 due to duplicated records without DISTINCT, which the count query creator strips out.
An obvious solution is to provide a custom count query. It works. But it also makes code hardly maintainable because I have to duplicate page-sized queries and just make them double: value
and countQuery
. I suggest a simple wrapper option that will take a value
and create a count query like
SELECT COUNT(dt.*) FROM (<value_query>) AS dt
It won't solve all issues for everyone, but at least it will allow users to not repeat themselves for 100x of queries.