HQL中的特殊字符 [英] special characters in HQL

查看:333
本文介绍了HQL中的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Hibernates HQL从mySql数据库获取查询 *

hi i'm trying to get from a mySql DB a query using Hibernates HQL *

public String getDetailsByUserAndFullPath(String user,String fullpath) {
    String temp2 = "%" + user + "%";
            String temp3 = "%" + fullpath + "%";
            // Query query =
            Query query_bd = session
                    .createQuery("from Files files  where user like ? and full_path like ? escape '\'");
            query_bd.setString(0, temp2);
            query_bd.setText(1, temp3);

            List<Files> list = query_bd.list();

问题是fullpath包含\,例如 c:\ temp \ example.docx 我使用转义符得到e查询错误 我确定这是一件小事,但我已经自杀了两个小时! 请帮忙 tnx

the problem is the fullpath contains \ such as c:\temp\example.docx and i get e query error using the escape i'm shure it's a small thing but i'm killing myself for two hours already!! please help tnx

推荐答案

\在您的语言(似乎是Java)中也是一个转义符

The \ is an escape also in your language (that seems to be Java)

所以查询看起来像这样.

so the query looks like this.

from Files files  where user like ? and full_path like ? escape ''

尝试添加另一个反斜杠

 from Files files  where user like ? and full_path like ? escape '\\'

如果您使用绑定参数,则不需要转义字符,我也不记得在某些DBMS中这是特殊字符.另外,还提供了转义参数以支持%_字符.

If you are using binded parameter you don't need to escape characters, also i don't recall that this is special character in some DBMS. Also the escape parameter is provided to support more likely the % or _ characters.

这篇关于HQL中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆