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>
解决: 推荐第一种解决方案
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>
相关文章推荐
- Hql传中文参数出现乱码(同sql传中…
- js传参到asp.net后台是中文参数出现乱码
- 在Ubuntu 10.04中文版下Qt编程,使用unixODBC和FreeTDS连接MS SQL Server 2005,并且中文不出现乱码的方法
- JSP中文参数传至JavaBean出现乱码
- PL/SQL Oracle客户端中文出现乱码的解决方法
- 关于struts2中传入中文参数然后显示到页面出现乱码
- js window.open 传递中文参数出现乱码解决办法
- springMVC 参数传递出现中文乱码
- hibernate3中hql中文参数乱码
- asp.net(VB)传递参数时出现中文乱码的问题
- Struts2中使用GET方式传递中文参数出现乱码的解决方案
- hibernate的sql语句中传中文参数乱码问题
- Spring Tomcat Post Get 请求参数有中文时出现乱码或+号变空格等关于编码的问题
- js提交表单中包含中文参数值,request请求参数正常,服务器端接收出现乱码解决思路
- hibernate之HQL传中文参数乱码 解决办法
- 在SQL 2005中用T-SQL插入中文数据时出现的问号或乱码的解决方案!!!!
- 使用AJAX传递中文参数,在后台接受出现乱码问题
- 在SQL Server 2005(2008)中用T-SQL插入中文数据时出现的问号或乱码的解决方案
- 使用AJAX传递中文参数,在后台接受出现乱码问题
- SpringMVC Controller 接收页面传递的中文参数出现乱码