MyException - 我的异常网
当前位置:我的异常网» Java Web开发 » hibernate like 中文查询有关问题,

hibernate like 中文查询有关问题,

www.MyException.Cn  网友分享于:2013-12-28  浏览:73次
hibernate like 中文查询问题,急!!!
我的查询代码如下:
Session   s   =   HibernateSessionFactory.getSession();
int   size;
try   {
size   =   s.createQuery(
"from   Repair   where       addres   like   '% "
+   new   String( "杭州 ".getBytes( "gb2312 "))
+   "% '     order   by     sort       desc ").list().size();
System.out.println(size);
}   catch   (HibernateException   e)   {
e.printStackTrace();
}   catch   (Exception   e)   {
e.printStackTrace();
}
生成的SQL如下:
Hibernate:   select   repair0_.id   as   id,   repair0_.protypeid   as   protypeid7_,   repair0_.name   as   name7_,   repair0_.loginname   as   loginname7_,   repair0_.explains   as   explains7_,   repair0_.password   as   password7_,   repair0_.addres   as   addres7_,   repair0_.phone   as   phone7_,   repair0_.chargename   as   chargename7_,   repair0_.isoutstanding   as   isoutst10_7_,   repair0_.sort   as   sort7_,   repair0_.vip   as   vip7_,   repair0_.protypes   as   protypes7_   from   sjfw.repair   repair0_   where   repair0_.addres   like   '%????% '   order   by   repair0_.sort   desc
0
中文出现乱码,查询结果不正确,请帮忙解决一下,谢谢!!!

------解决方案--------------------
在hibernate 的 properties文件中改:
hibernate.query.factory_class : org.hibernate.hql.classic.ClassicQueryTranslatorFactory
hibernate.query.dialect : org.hibernate.dialect.HSQLDialect

或者 在hibernate 的 cfg.xml中加:
<property name= "factory_class "> org.hibernate.hql.classic.ClassicQueryTranslatorFactory </property>
<property name= "dialect "> org.hibernate.dialect.HSQLDialect </property>
------解决方案--------------------
你的数据库中存储的字符集是什么呢?一般默认ISO8859-1
------解决方案--------------------
session.createQuery( "from Repair where addres like :address ")
.setString( "address ", "% "+杭州+ "% ")
不喜欢拼 sql语句
------解决方案--------------------
字符集不匹配,去掉 ".getBytes( "gb2312 ")试一下
------解决方案--------------------
Session s = HibernateSessionFactory.getSession();
int size;
try {
size = s.createQuery(
"from Repair where addres like '% "
+ "杭州 "
+ "% ' order by sort desc ").list().size();
System.out.println(size);
} catch (HibernateException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

------解决方案--------------------
getBytes( "ISO-8859-1 ", "GB2312 ")
------解决方案--------------------
这个是hibernate3.1版本的一个bug,3.2已经修复了这个问题

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有