Skip to content
Advertisement

Dynamic search term SQL query with Spring JPA or QueryDSL

I am trying to learn QueryDSL in order to return the results of a single search term from Postgres:

JavaScript

But I also want to search for any number of search terms, for example, say this is my list of search terms divided by the plus symbol:

JavaScript

How can I dynamically create contains(params[0]) AND/OR contains(params[1] AND/OR … contains(params[n]) using either QueryDSL or any Java/Spring query framework? I see QueryDSL has a predicate system, but I still don’t understand how to dynamically create a query based on a variable number of parameters searching in the same column.

Advertisement

Answer

I figured it out. It’s a little non-intuitive, but using BooleanBuilder and JPAQuery<?> you can create a dynamic series of boolean predicates, which return a list of matches.

Example:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement