Hump ​​projection and loss of meta-information

When using projection on properties, the result is returned as a list with elements in the same sequence as in the forecast block. At the same time, property names are not in the list, and this is really disadvantageous for the developer, since the result will be transmitted together, and the caller needs to know what value belongs to which property. Is there a way to return a map from a Criteria request with the property name as the key to the value?

so, the following code:

def c = Trade.createCriteria() def remicTrades = c.list { projections { property('title', 'title') property('author.name', 'author') } def now = new Date() between('publishedDate', now-365, now) } 

This returns:

 [['book1', 'author1']['book2', 'author2']] 

Instead, I would like it to return:

 [[book:'book1', author:'author1'][book:'book2', author:'author2']] 

I know that I can organize this path after receiving the result, but I sincerely believe that the property alias should have been used by the criteria to return a list of a map that imitates the result of an SQL query, not a list. / P>

+8
grails gorm createcriteria
source share
2 answers

Duplicate: Grails criteria queries: how to return a map with a column?
And the corresponding answer (and solution): stack overflow

Use resultTransformer.

 import org.hibernate.criterion.CriteriaSpecification Trade.withCriteria { resultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP) projections { property('title', 'title') property('author.name', 'author') } def now = new Date() between('publishedDate', now-365, now) } 
+8
source share

Agree with the arguments of your question, this really should be part of the main GORM solution. However, here is my workaround;

 def props = ['name','phone'] def query = Person.where {}.projections { props.each{ property(it) } } def people = query.list().collect{ row-> def cols = [:] row.eachWithIndex{colVal, ind-> cols[props[ind]] = colVal } cols } println people // shows [['name':'John','phone':'5551212'],['name':'Magdalena','phone':'5552423']] 
+1
source share

All Articles