I need to pass in a variable number of parameters to a spring/JPA repo to mimick something like this.
select * from myTable
where name like '%SOME_VALUE%'
or name like '%SOME_OTHER_VALUE%'
or name like '%SOME_OTHER_VALUE2%'
or an unknown number of other values
So far I haven't been able to determine what the correct way to to do this is. I'm using Spring 4.3.7, Hibernate 5.2.9, and Spring Data 1.11.1. I've googled around and it doesn't appear that there's way to do this with a normal CRUD repo, but so far I haven't found any examples that look like what I need. I think CriteriaBuilder
is what I am supposed to use, but that seems to have fallen out of favor, so I'm not sure what the correct way to do this is.
So I followed @Jorge Campos advice and used specification. My code looks like this now:
And my repo definition is this.
Perhaps you are looking for something like this?:
Source.