special characters in HQL

2019-06-10 01:55发布

问题:

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.