def criteria = User.createCriteria()
def users = criteria.list() {
    projections {
        distinct("username")
    }
    setResultTransformer(CriteriaSpecification.ROOT_ENTITY)
}
只需将 setResultTransformer(CriteriaSpecification.ROOT_ENTITY) 替换为 resultTransformer(ALIAS_TO_ENTITY_MAP)。结果你会得到一个字符串列表
否则只需将 .list 替换为 .listDistinct 并使用不需要 distinct("username"),只需 property("username");
通常人们会遇到分页问题。不是结果。如果你已经有类似的东西:
User.createCriteria().list([max:params.max,offset:params.offset],{
createAlias("others", "others", CriteriaSpecification.LEFT_JOIN);
ilike("others.firstName", "%${query}%");
});
它可能导致行重复。因为 .listDistinct() 不支持分页,所以加
resultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
所以查询将如下所示:
User.createCriteria().list([max:params.max,offset:params.offset],{
    resultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    createAlias("others", "others", CriteriaSpecification.LEFT_JOIN);
    ilike("others.firstName", "%${query}%");
    });