How do you exactly create dynamic JPQL queries. I have a repository interface and I can call the declared methods. The problem with that is if I have a filtering requirement from the presentation layer I wouldn't know in advance which fields are chosen by the user. With the repository interface it expects we declare already the fields that we'll be using.
Of course I could probably declare all the possible combination of fields and declare them as methods in the repository and probably have one big if-else statement. But I think that's bad. A JPQL query, like HQL, would be better for this scenario.
Is this possible? I've checked the docs and all I see are @Query and @NamedQuery but you have to declare it already in the repository interface. Is there a way to pass that query dynamically?
For example I have the following JPQL query:
How do you pass that query without declaring in the repository?
select u from User u where u.emailAddress = ?1 and u.lastname = ?2