special characters in HQL

霸气de小男生 提交于 2019-12-08 11:22:27

问题


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();

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


回答1:


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 ''

Try to add another backslash

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

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.



来源:https://stackoverflow.com/questions/6264057/special-characters-in-hql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!