奇怪的hibernate createSQLQuery返回CHAR |字符但字符串

问题描述:

我正在使用hibernate createSQLQuery,但它只返回第一个字符。奇怪的hibernate createSQLQuery返回CHAR |字符但字符串

tableName = 'select code, name from airports where excludeFromSearch = 0 order by name'; 
s.createSQLQuery(tableName).list(); 

我使用:

  • Mysql的
  • 冬眠3.2

快照:

enter image description here

我用Google搜索stackoverflowforum.hibernateatlassian

UPDATE:我还检查了所有的Hibernate查询日志,甚至MySQL查询日志都是一样的。

`code char(3)` // <strike>might</strike> must cause problem as in screen shot 
name varchar(100) 
+4

可能重复[休眠本地查询 - CHAR(3)列(http://stackoverflow.com/questions/4873201/hibernate-native-query-char3-column) – axtavt 2011-06-15 10:17:34

+0

@axtavt你是对的,哥们 – 2011-06-15 10:28:48

首先,尝试在数据库上激发由hibernate生成的查询,然后尝试匹配结果。如果对数据库的查询也返回相同的结果,则检查您的表定义,代码实际上是字符串或其他。

更好的选择来编写SQL查询的查询将他们写在HBM文件,然后在其中定义返回属性,这将帮助你很多,因为那么你将能够隐藏输出到所需的对象,而不是对象的数组。

您是否尝试将Scalar设置为您的请求?

StringType stringTypeInstance = StringType.class.newInstance(); 

String tableName = 'select code, name from airports where excludeFromSearch = 0 order by name'; 

List<Object[]> result = s.createSQLQuery(tableName) 
    .addScalar("code",stringTypeInstance) 
    .addScalar("name",stringTypeInstance) 
    .list(); 

你可以在SQL语句中简单转换。当炭自动连接到字符列值在Hibernate老的版本

select convert(varchar, Name) from Person

存在问题。 https://hibernate.atlassian.net/browse/HHH-2304