简体   繁体   中英

JPQL LIKE syntax with Strings

EDIT:

I changed the hard coded query to be:

 query.setParameter("desc", "%unplug //your// server... enjoy the freedom%" ESCAPE '//')

and now I am getting an com.sun.jdi.InvocationException occurred invoking method.


There's no stacktrace produced either.




I have a description column in my PostgreSQL database and I am trying to query it with a 'LIKE' clause, however I am unable to get any results. Here's an example:

    Query query = em.createQuery("from MyClass c WHERE c.description LIKE :desc");
    query.setParameter("desc", "%unplug /your/ server... enjoy the freedom%"); 

In the database I have many descriptions containing a substring of the above text. I've done a lot of research and looked into escaping special chars etc, but nothing has worked.

I am missing something, I just cannot figure out what that is.

Most likely the slash / is messing up the parsing in PG. Turn the parameter into a quoted literal. Unless you are certain that no special characters go into string arguments (ie you control the strings), this is always a good idea to avoid SQL injection.

query.setParameter("desc", "quote_literal('%unplug /your/ server... enjoy the freedom%')");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM