I am trying to write a JPQL query with a like clause:
LIKE '%:code%'
I would like to have code=4 and find
455 554 646 ...
I cannot pass :code = '%value%'
namedQuery.setParameter("%" + this.value + "%");
because in another place I need :value
not wrapped by the %
chars. Any help?
I don't know if I am late or out of scope but in my opinion I could do it like:
If you do
and then do
Then value remains free from the '%' sign. If you need to use it somewhere else in the same query simply use another parameter name other than 'code' .
Just leave out the ''
There is nice like() method in JPA criteria API. Try to use that, hope it will help.
I don't use named parameters for all queries. For example it is unusual to use named parameters in JpaRepository.
To workaround I use JPQL CONCAT function (this code emulate start with):
I found this technique in excellent docs: http://openjpa.apache.org/builds/1.0.1/apache-openjpa-1.0.1/docs/manual/jpa_overview_query.html
You could use the JPA LOCATE function.
FYI: The documentation on my top google hit had the parameters reversed.