-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: like fails with MySQL if string has back slash
PostPosted: Fri Aug 19, 2005 10:40 pm 
Newbie

Joined: Thu Mar 31, 2005 11:59 pm
Posts: 10
problem description: With Hibernate 3.0 and MySQL 5.0 queries with the like operator return no results if term searched for contains a backslash. All queries perform without throwing any errors.

With records having values ""java", "carl.java", "dir2\\carl.java", and
"d:\\dir1\\carl.java", the find equal query correctly returns the appropriate record.

However the findEndsWith query returns 0 records for when searching on any pattern containing a back slash such as "dir2\\carl.java"

Since the quality test works, I claim that my string values are ok.

from the class TestThing:

Code:
public static List findEq (String key)throws Exception {
        String query = "from TestThing where path = :key";
        logger.info ("find equal " + key);
        return find (query, key);
    }
   
    public static List findLikeEnd (String key) throws Exception {
        String query = "from TestThing where path like :key";
        logger.info ("find like end " + key);
        return find (query, "%" + key);
    }
   
    private static List find(String query, String key) throws Exception {
        Session session = Setup.getSession();
       
        Query q = session.createQuery(query).setString("key", key);
        List results = q.list();
        logger.info ("result size = " + results.size());
       
        return results;
    }



from the test fixture:

Code:
public void testing() throws Exception {
        logger.info("starting testing");
       
       
        for (String p : paths){
            TestThing thing = new TestThing();
            thing.setPath(p);
            thing.save();
        }
       
        for (String p : paths){
            logger.info("for path = " + p);
            List eqList = TestThing.findEq(p);
            showList(eqList, "equal", p);
           
        }
       
        for (String p : paths){
            List endList = TestThing.findLikeEnd(p);
            showList(endList, "ends with", p);
        }
       
    }
   
    private void showList(List list, String label, String key){
        System.out.println("----------------------------------");
        System.out.println("results for " + label + " " + key);
        System.out.println("  size = " + list.size());
        for (Object o : list){
            TestThing thing = (TestThing)o;
            System.out.println ("  " + thing.getPath());
        }
    }


Hibernate version: 3.0

Mapping documents:

Code between sessionFactory.openSession() and session.close():


Name and version of the database you are using:MySQL 5.0 beta

The generated SQL (show_sql=true):
Hibernate: select testthing0_.id as id, testthing0_.path as path5_ from thing testthing0_ where testthing0_.path=?

and
Code:
Hibernate: select testthing0_.id as id, testthing0_.path as path5_ from thing testthing0_ where testthing0_.path like ?


Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 20, 2005 12:15 pm 
Newbie

Joined: Thu Mar 31, 2005 11:59 pm
Posts: 10
OK, I can make the like query work if I double all the back slashes in the value being matched. I don't have to do this in the equal query.

So the code now works but I don't understand the rules.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.