简体   繁体   English

休眠条件。列出带有引号和重音的不良结果

[英]Hibernate criteria.list bad results with quotes and accents

I'm using hibernate 3.6 and i'm having problems using criteria.list. 我正在使用休眠3.6,并且在使用criteria.list时遇到问题。 I need search words with quotes or accents like "d'eau" or "d´eau". 我需要带有“ d'eau”或“ d´eau”等引号或口音的搜索词。 My code is something like this: 我的代码是这样的:

Criteria criteria;
criteria.add(Restrictions.ilike("nameParam", "d''eau", MatchMode.ANYWHERE));

I put two single quotes because i'm working with a sql server database. 我用两个单引号引起来,因为我正在使用sql server数据库。 There are 2 single quotes because single quote escapes with another single quote. 有2个单引号,因为单引号与另一个单引号一起转义。 The statement has 0 results, but if I execute the sql statement printed in the log in the sql server client, I get 120 results aprox. 该语句有0个结果,但是如果我执行sql服务器客户端中的日志中打印的sql语句,则会得到120个结果aprox。

Testing with HQL and the same sql statement. 使用HQL和相同的sql语句进行测试。 I get this: 我得到这个:

String hqlQuery = "select distinct(t) from Table t where b.idCon in (select t2.idCon from Table t2 where lower(t2.outTerTb) like '%d''eau%')";
List qwer = getEntityManager().createQuery(hqlQuery).getResultList();
System.out.println("qwer.size() -> " + qwer.size());

String hqlQuery2 = "select distinct(t) from Table t where b.idCon in (select t2.idCon from Table t2 where lower(t2.outTerTb) like :param)";
List qwer2 = getEntityManager().createQuery(hqlQuery2).setParameter("param", "%d''eau%").getResultList();
System.out.println("qwer2.size() -> " + qwer2.size());

This code print: 此代码打印:

qwer.size() -> 120
qwer2.size() -> 0

And I don't understand why this happens. 而且我不明白为什么会这样。 Sorry if my english is bad 对不起,如果我的英语不好

You don't need to escape single quotes in your parameters. 您无需在参数中转义单引号。 That's the whole point of using parameters (in Hibernate and, behind the scenes, in the JDBC prepared statements): the JDBC driver escapes everything that needs to be escaped for you. 这就是使用参数的全部要点(在Hibernate中以及在后台,在JDBC Prepared语句中):JDBC驱动程序转义了需要为您转义的所有内容。

You take what comes from the UI layer as is and stuff it into parameters, everything isproperly escaped by the driver, and you don't risk any SQL injection attack. 您可以直接使用来自UI层的内容,并将其填充到参数中,所有内容都可以由驱动程序正确地转义,并且无需冒任何SQL注入攻击的风险。

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

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