Parameter in like clause JPQL

Manuele Piastra picture Manuele Piastra · Aug 27, 2009 · Viewed 118.3k times · Source

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?

Answer

shipmaster picture shipmaster · Aug 28, 2009

If you do

LIKE :code

and then do

namedQuery.setParameter("code", "%" + this.value + "%");

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' .