I am facing a problem to get the required result from this closure

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

        and{
            ...
            ...
        }
    }

    [authors:results]
}

I want to show this list on my gsp page and wants to access the values using aliases (while above criteria is returning a list of arrays)

有帮助吗?

解决方案

Use 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')
        }
    }
}

All projections must have aliases. Otherwise the resulting map will contain nulls.

其他提示

You can try this

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]   }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top