2011-08-17 34 views
1
突出問題

我面臨的一個問題,從這個封閉得到所需要的結果我現在面臨與個createCriteria

def authors{ 
    results = Message.createCriteria().list { 
     projections { 
      author{ 
       groupProperty('id', 'authorId') // 2nd param is alias 
       property('username', 'username') 
      } 
     } 

     and{ 
      ... 
      ... 
     } 
    } 

    [authors:results] 
} 

我想表明我 GSP頁面上這份名單,並希望通過訪問值別名 (當上述標準被返回數組列表)

回答

5

用途使用ResultTransformer(Criter iaSpecification.ALIAS_TO_ENTITY_MAP)。

import org.hibernate.criterion.CriteriaSpecification 
Message.createCriteria().list { 
    resultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP) 
    projections { 
     author{ 
      groupProperty('id', 'authorId') 
      property('username', 'username') 
     } 
    } 
} 

所有投影必須有別名。否則,結果地圖將包含空值。

+0

提示所有投影必須有別名。我瘋了,爲什麼我得到空值。 –

3

你可以試試這個

def authors{ 
    results = Message.createCriteria().list { 
     projections { 
      author{ 
       groupProperty('id') 
       property('username') 
      } 
     } 

     and{ 
      ... 
      ... 
     } 
    } 
    List authors = results.collect{record -> [authorId : record[0], username:record[1]} 
    [authors:authors] } 
+0

謝謝,它解決了我的問題。 –

+0

然後接受答案! – hitty5