我的查询代码如下:
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
中文出现乱码,查询结果不正确,请帮忙解决一下,谢谢!!!
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.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>
.setString("address","%"+杭州+"%")
不喜欢拼 sql语句
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();
}