I have a somewhat complicated @Query in a JpaRepository.
I need to get the results of this query in two forms (but not at the same time!):
- First, the client asks for a count of the number of results:
SELECT COUNT(x.*) FROM my_table x ...
- Then later (maybe), they want to see the actual data:
SELECT x.* FROM my_table x ...
What follows (the ...
) is identical for both queries. Is there any way to combine these so that I don't repeat myself?
- I know I could just use the second method, and count the number of elements in the resulting List. However, this adds the overhead of actually fetching all those elements from the database.
- I could put the
...
in a String constant somewhere, but that kind of separates it from its context (I'd lose IntelliJ's syntax highlighting/error checking) - I can't convert it to a Criteria or Example query, because I need to use PostGIS's geography type. (And these are less readable anyway...)
Any other ideas?