ceklock
ceklock

Reputation: 6372

JPA query LIKE does not work

What is wrong with this query: SELECT i FROM Item i WHERE 1 = 1 AND UPPER(i.nome) LIKE %:nome% ORDER BY UPPER(i.nome)? I tried with and without single quotes in LIKE '%:nome%' but it doesn't work.

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing [SELECT i FROM Item i WHERE 1 = 1  AND UPPER(i.nome) LIKE %:nome%  ORDER BY UPPER(i.nome)]. 
[56, 63] The identification variable '%:nome%' is not following the rules for a Java identifier.
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605)

Upvotes: 0

Views: 1464

Answers (1)

Darshan Mehta
Darshan Mehta

Reputation: 30849

As explained here, you need to wrap the value with % while setting the parameters, e.g.:

query = em.createQuery("SELECT i FROM Item i WHERE 1 = 1  AND UPPER(i.nome) LIKE :nome  ORDER BY UPPER(i.nome)");
query.setParameter("nome","%"+nome.toUpperCase()+"%");

Upvotes: 1

Related Questions