简体   繁体   中英

How to escape from '!' symbol in HQL

Can any one please help to escape from NOT(!) symbol in HQL. I am using Postgres 8.3. Also I am unable to search with these chars |\\?'( )

fts('english',COLUMN_NAME,'" + searchText + "') = true)

You should be binding the search text instead of concatenating it manually to the query. Not only does it fixes your problem, it's also better in terms of security (SQL injection) and performance (the database sees the same query with different parameters, which allows it to cache the compiled query when it can).

Change your query so it looks like:

fts('english', COLUMN_NAME, :searchText) = true // using named parameters
// or
fts('english', COLUMN_NAME, ?) = true // using positional parameters

You can then properly bind your parameters:

session.createQuery(hqlQuery)
        .setString("searchText", searchText)
        .iterate();
// or
session.createQuery(hqlQuery)
        .setString(0, searchText)
        .iterate();

It's the same principle as using a PreparedStatement with JDBC and setting the parameters (the main difference being that the parameters' indices start at 1 with JDBC and 0 with HQL).

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