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已经修复了这个问题