Reuben Peter-Paul
Reuben Peter-Paul

Reputation: 1580

How to specify a JPA named parameter surrounded by wildcards?

How would I specify a JPA query like:

Query q = 
  em.createQuery(
    "SELECT x FROM org.SomeTable x WHERE x.someString LIKE '%:someSymbol%'"
  );

followed by:

q.setParameter("someSymbol", "someSubstring");

and not triggering a

org.hibernate.QueryParameterException: could not locate named parameter [id]

Much appreciated!

Upvotes: 64

Views: 61509

Answers (3)

Chinmoy
Chinmoy

Reputation: 1456

query.setParameter(someSymbol, "%" + someSymbol+ "%"); 

Upvotes: 4

xtian
xtian

Reputation: 3169

For reference, you could also use CONCAT:

like CONCAT('%', :someSymbol, '%')

Upvotes: 71

musiKk
musiKk

Reputation: 15189

How about

Query q = 
  em.createQuery(
    "SELECT x FROM org.SomeTable x WHERE x.someString LIKE :someSymbol"
);
q.setParameter("someSymbol", "%someSubstring%");

I'm pretty sure I once solved your problem like that.

Upvotes: 95

Related Questions