Home  >  Article  >  Database  >  框架技术-hiberntae查询mysql库字段是char类型的有关问题

框架技术-hiberntae查询mysql库字段是char类型的有关问题

WBOY
WBOYOriginal
2016-06-07 16:24:441186browse

框架技术--hiberntae查询mysql库字段是char类型的问题 hiberntae查询mysql库字段是char类型时,查询出的字段只有1个字母 如查询user表,name字段(char类型),为zhangsan 但是查询出的结果为:z 经网上收集资料了解到时hibernate在对原生SQL进行映射时出现的

框架技术--hiberntae查询mysql库字段是char类型的问题

hiberntae查询mysql库字段是char类型时,查询出的字段只有1个字母

如查询user表,name字段(char类型),值为zhangsan

但是查询出的结果为:z


经网上收集资料了解到时hibernate在对原生SQL进行映射时出现的问题。


解决方法,在查询出的解决过增加query.addScalar即可解决此问题

注意,使用了addScalar,就要将所有的字段都指定类型。


代码:

		StringBuffer sb = new StringBuffer();
		sb.append("select opcode, opname, parent_opcode, menu_level, top_parent_opcode, "
				+ "top_top_parent, system_type, haschild, url "
				+ "from ad_op_code_right ald where 1=1");
		sb.append(conditions);
		if (pageVo != null) {
			PageUtil.generateConditions(pageVo, sb);
		}
		SQLQuery query = session.createSQLQuery(sb.toString());
		List<object> aldList = new ArrayList<object>();
		List<ad_op_code_rightdto> opCodeList = new ArrayList<ad_op_code_rightdto>();
		// 手动映射,解决hibernate 对char类型字段映射问题
		query.addScalar("opcode",Hibernate.INTEGER);
		query.addScalar("opname",Hibernate.STRING);
		query.addScalar("parent_opcode",Hibernate.INTEGER);
		query.addScalar("menu_level",Hibernate.BYTE);
		query.addScalar("top_parent_opcode",Hibernate.INTEGER);
		query.addScalar("top_top_parent",Hibernate.INTEGER);
		query.addScalar("system_type",Hibernate.BYTE);
		query.addScalar("haschild",Hibernate.BYTE);
		query.addScalar("url",Hibernate.STRING);
		aldList = query.list();</ad_op_code_rightdto></ad_op_code_rightdto></object></object>

参考网站:

http://cst.is-programmer.com/posts/29331.html.

http://www.fx114.net/qa-251-2954.aspx

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn