У меня проблема с проекциями в createCriteria - PullRequest
1 голос
/ 17 августа 2011

Я столкнулся с проблемой, чтобы получить требуемый результат от этого закрытия

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

        and{
            ...
            ...
        }
    }

    [authors:results]
}

Я хочу показать этот список на моей странице gsp и хочу получить доступ к значениям, используя псевдонимы (в то время как вышеуказанные критерии возвращаютсписок массивов)

Ответы [ 2 ]

6 голосов
/ 07 мая 2013

Использовать 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')
        }
    }
}

Все проекции должны иметь псевдонимы. В противном случае полученная карта будет содержать нули.

3 голосов
/ 17 августа 2011

Вы можете попробовать это

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]   }
...