1

我面临着从这个关闭中获得所需结果的问题

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

        and{
            ...
            ...
        }
    }

    [authors:results]
}

我想在我的 gsp 页面上显示此列表,并希望使用别名访问值(而上述条件返回数组列表)

4

2 回答 2

6

使用 resultTransformer(CriteriaSpecification.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')
        }
    }
}

所有投影都必须有别名。否则生成的地图将包含空值。

于 2013-05-07T01:07:01.977 回答
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]   }
于 2011-08-17T05:44:56.570 回答