How to implement long/complex query with spring-data-jpa 2.1
Derived queries, i.e. those deriving the actual query from the method name, are the wrong tool for such long or complex queries because the resulting name becomes unusable.
Alternatives that you should consider are
using a fixed query provided in a
@Query
annotation: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.at-queryusing a named query where you provide the query on the entity: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.named-queries
using a specification where you dynamically assemble the where clause: https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
You have below options to do that. 1) use hql , for that it is mandatory to have all the tables mapped as jpa entities 2) is to use native queries, the downside here is that it affects the portability of the application, but if u r sure that ur application is not going to migrate on any other database then u can use this 3) is to use criteriabuilder
Edit-> Found our that @Query can be used for both SQL and JPQL to execute .
Please follow below link for more information
https://www.baeldung.com/spring-data-jpa-query
Usage of Query in some spring developer certification books also.
Hope that helps