Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 带条件的Grails查询:如何获取带列的映射?_Hibernate_Grails_Createcriteria - Fatal编程技术网

Hibernate 带条件的Grails查询:如何获取带列的映射?

Hibernate 带条件的Grails查询:如何获取带列的映射?,hibernate,grails,createcriteria,Hibernate,Grails,Createcriteria,是否可以使用条件查询grails并接收地图列表而不是列表列表?我希望在结果中包含列名,以便使用“关联数组”而不是数字数组偏移量。我现在做的是 def topFiveUsers = BlogEntry.createCriteria().list { projections { count('id') groupProperty('author') } maxResults 5 } 这将

是否可以使用条件查询grails并接收地图列表而不是列表列表?我希望在结果中包含列名,以便使用“关联数组”而不是数字数组偏移量。我现在做的是

    def topFiveUsers = BlogEntry.createCriteria().list {
        projections {
            count('id')
            groupProperty('author')
        }
        maxResults 5
    }
这将导致
[[123,app.User:1][111,app.User:2][……]
,即列表列表。我更喜欢像
[[posts:123,author:app.User:1][posts:111,author-app.User:2][……]

一如既往:非常感谢您的帮助

def topfifelist=[]
def topFiveList = []
topFiveUsers.each { rec ->
    topFiveList << [posts: rec[0], author: rec[1]]
}
topFiveUsers.each{rec-> topFiveList使用。 作为参数使用
关于此主题的文档和示例很少。但这里有一个示例:

import org.hibernate.criterion.CriteriaSpecification

BlogEntry.withCriteria {
  maxResults 5
  resultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP)
  projections {
    count('id', 'total')
    groupProperty('author', 'author')
  }      
}  

请注意,所有投影都需要别名。否则,生成的映射将由空值组成。

感谢您的回复。当然,我可以稍后转换结果-这就是我目前正在做的。然后,我想再次跳过对结果集进行后处理的开销。我想hibernate代码中的某个地方也有相同的转换不管怎样阵型都会发生…在我发布后不久,我突然意识到你对两步解决方案不感兴趣。不用担心!无论如何谢谢你的支持!se太棒了!才华横溢,优雅。非常感谢Sergey。
import org.hibernate.criterion.CriteriaSpecification

BlogEntry.withCriteria {
  maxResults 5
  resultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP)
  projections {
    count('id', 'total')
    groupProperty('author', 'author')
  }      
}