2011-06-15 60 views
2

我正在使用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

回答

2

首先,嘗試在數據庫上激發由hibernate生成的查詢,然後嘗試匹配結果。如果對數據庫的查詢也返回相同的結果,則檢查您的表定義,代碼實際上是字符串或其他。

更好的選擇來編寫SQL查詢的查詢將他們寫在HBM文件,然後在其中定義返回屬性,這將幫助你很多,因爲那麼你將能夠隱藏輸出到所需的對象,而不是對象的數組。

6

您是否嘗試將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();