您的位置:首页 > 数据库

hql/sql传中文参数出现乱码

2012-10-13 15:35 337 查看
问题:如题

解决:  推荐第一种解决方案

1.占位符

可以使用?或者:***的方式在外部配置查询参数,如:

Query query=session.createQuery("from TestStu s where s.team=:team and s.age>:age"); 或者

Query query=session.createQuery("from TestStu s where s.team=? and s.age>?);

这句话的意思是从一个持久的team对象中,取出其持有的TestStu集合,并筛选出age大于执行数据的记录

我们可以这样设置参数

query.setParameter("team",team,Hibernate.entity(TestTeam.class));

 //或者使用query.setEntity("team",team);

query.setParameter("age", 15);

但我们决不能在HQL中又出现?,又出现变量占位符,即

Query query=session.createQuery("from TestStu s where s.team=? and s.age>:age);

这样,在设置参数时候,会出现异常如下:

 cannot define positional parameter after any named parameters have been defined [from Search.filter.TestStu s where s.team=:team and s.age>?] 
2.配置文件

在spring配置文件中加入红色部分
<bean id="sessionFactory"

   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

   ........

   <property name="hibernateProperties">

    <props>

     <prop key="hibernate.dialect">

      org.hibernate.dialect.SQLServerDialect

     </prop>

    <prop key="hibernate.query.factory_class">

             org.hibernate.hql.classic.ClassicQueryTranslatorFactory    

      </prop>     

    </props>

   </property>

.............

</bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息