---
title: "hibernate中文查询问题~学习笔记"
author: "rfheh"
date: "2011-09-21T11:14:57Z"
tags: []
categories: ["编程语言"]
slug: "hibernate"
draft: false
lastmod: "2011-09-21T11:14:57Z"
id: 1176882
url: "https://rfheh.iteye.com/blog/1176882"
---

一、Hibernate对中文查询有Bug，所以不能直接写在HQL语句中，比如说：

from cat where cat.name like ‘你好’

这样是可行的：

String hql="from table as t where t.name like ？and t.address like ? and t.title=?";

String parameter={new String(“%”+myName+”%”), new String(“%”+myAddress+”%”), new String(“%”+myTitle+”%”)};

this.getHibernateTemplate().find(hql,paramter);

二、如果你真想用中文查询  而且用字符串连接的话：

改个配置文件： 

hibernate当中使用hql进行查询的时候，如果参数是中文，这hql打印为乱码。以前从来没有遇到过的问题，郁闷。经过无数次的尝试之后都无法解决，查找资料突然发现这个hibernate3的一个bug。在hibernate3当中使用antlr来解析hql，估计是这个的问题。
解决方法为：
1、采用占位符，然后setString
2、将 hibernate.query.factory_class 属性设置成 org.hibernate.hql.classic.ClassicQueryTranslatorFactory
 
 
三、推荐使用：
 public List getJbtj(String tbname){
   // System.out.print(str);
      list = this.getHibernateTemplate().find("from Jbtj where tbname=?",new Object{tbname});
      System.out.print(list.size());
       return list;
  }
 
我用有效！！！