简体   繁体   English

如何逃脱“!” HQL中的符号

[英]How to escape from '!' symbol in HQL

Can any one please help to escape from NOT(!) symbol in HQL. 谁能帮助摆脱HQL中的NOT(!)符号。 I am using Postgres 8.3. 我正在使用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). 它不仅可以解决您的问题,而且在安全性(SQL注入)和性能方面也更好(数据库看到具有不同参数的同一查询,从而可以在可能的情况下缓存已编译的查询)。

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). 这与在JDBC中使用PreparedStatement并设置参数的原理相同(主要区别在于,对于JDBC,参数的索引从1开始,对于HQL,参数的索引从0开始)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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