在数据库中存储Windows路径并使用java检索Hibernate [英] Storing Windows Path in Database and retrieving with Hibernate using java

查看:105
本文介绍了在数据库中存储Windows路径并使用java检索Hibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用java如果我在h2数据库中存储windows路径,然后通过匹配文件名
来尝试和检索使用Hibernate的对象

  public static List< Object []> findSongsRecNoAndModifiedDateWithinFolder(Session session,String folder)
{
Query q = session.createQuery(select recNo,filename,lastModified from Song t1 where t1.filename like:filename);
q.setParameter(文件名,文件夹+'%',StandardBasicTypes.STRING);
List< Object []> results =(List< Object []>)q.list();
返回结果;
}

我没有找到匹配。



当我添加到数据库并尝试检索它时,如果我将所有'\'转换为'/',但这有点痛苦,我试图忽略UNIX和Windows之间的区别文件路径。



所以这个问题与Windows文件分隔符'\'有关,它也是Java(和数据库)转义字符。但是我真正不明白的是Java,数据库或hibernate的问题,以及我是否可以解决它。 解决方案

请参阅 http://groups.google.com/group / h2-database / browse_thread / thread / 390eed5e21584faa?pli = 1 为您的问题的答案: \ 是H2中的默认转义字符,而您如果您在类似的子句中使用它,则需要将其转义(使用另一个 \ )。

或者您可以将默认转义字符设置为空字符串以避免此转义问题。请参阅 http://www.h2database.com/javadoc/org/h2/常量/ DbSettings.html 并搜索defaultEscape。


Using java If I store windows paths in a h2 database, and then try and retrieve the objects using Hibernate by matching on the filename i.e

public static List<Object[]> findSongsRecNoAndModifiedDateWithinFolder(Session session, String folder)
    {
        Query q = session.createQuery("select recNo, filename, lastModified from Song t1 where t1.filename like :filename");
        q.setParameter("filename", folder+'%', StandardBasicTypes.STRING);
        List<Object[]> results = (List<Object[]>)q.list();
        return results;
    }

I get no matches.

If I convert all the '\' with '/' when I add to the database and try and retrieve it all works, but this is a bit of a pain I was trying to ignore the differences between UNIX and Windows files paths.

So the problem is something to do with Windows file separator '\' also being Java (and Database) escape character. but what I don't really understand is the problem with Java, the database or hibernate and if I can work round it.

解决方案

See http://groups.google.com/group/h2-database/browse_thread/thread/390eed5e21584faa?pli=1 for an answer to your question: \ is the default escape character in H2, and you need to escape it (with another \) if you use it in a like clause.

Or you can set the default escape character to an empty string to avoid this escaping problem. See http://www.h2database.com/javadoc/org/h2/constant/DbSettings.html and search for "defaultEscape".

这篇关于在数据库中存储Windows路径并使用java检索Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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