flu*_*xon 13 grails hibernate createcriteria
是否可以使用条件查询grails并接收地图列表而不是列表列表?我想在结果中使用列名称,然后使用"关联数组"而不是数组数组偏移.我现在做的事情就像
def topFiveUsers = BlogEntry.createCriteria().list {
projections {
count('id')
groupProperty('author')
}
maxResults 5
}
Run Code Online (Sandbox Code Playgroud)
结果[[123, app.User:1][111, app.User:2][...]...]
,即列表列表.我宁愿想要类似的东西[[posts:123, author: app.User:1][posts: 111, author app.User:2][...]...]
.
一如既往:非常感谢帮助!
Ser*_*yev 30
使用resultTransformer().作为参数使用CriteriaSpecification.ALIAS_TO_ENTITY_MAP
关于此主题的文档和示例很少.但这是一个例子:
import org.hibernate.criterion.CriteriaSpecification
BlogEntry.withCriteria {
maxResults 5
resultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP)
projections {
count('id', 'total')
groupProperty('author', 'author')
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,所有投影都需要别名.否则,生成的映射由空值组成.
def topFiveList = []
topFiveUsers.each { rec ->
topFiveList << [posts: rec[0], author: rec[1]]
}
Run Code Online (Sandbox Code Playgroud)