Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gorm projection and loss of metainformation

When using projection on the properties, the result is returned as the list with the elements in the same sequence as that defined in the projections block. At the same time the property names are missing from the list and that is really disadvantageous to the developer as the result would be passed along and the caller needs to know what value belongs to which property. Is there a way to return a map from the Criteria query with 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 I can arrange this way after getting the result but I earnestly feel that the property alias should have been used by the criteria to return a list of map that mimics the result of the SQL query and not a bland list.

like image 225
rks Avatar asked Nov 07 '12 00:11

rks


People also ask

What does Gorm stand for?

Gorm ( Graphical Object Relationship Modeller) is a graphical user interface builder application. It is part of the developer tools of GNUstep. Gorm is the equivalent of Interface Builder that was originally found on NeXTSTEP, then OPENSTEP, and finally on Mac OS X.

How do I retrieve a single object from a Gorm?

Retrieving a single object GORM provides First, Take, Last method to retrieve a single object from the database, it adds LIMIT 1 condition when querying the database, and it will return error ErrRecordNotFound if no record found.

What is the difference between Gorm for Neo4j and GraphQL?

The GORM GraphQL library provides functionality to generate a GraphQL schema based on your GORM entities Built for managing massive amounts of data in the cloud. GORM for Cassandra lets you map your objects to Cassandra tables. Built for Neo4j 3.0 and above, GORM for Neo4j is built on the native Neo4j Bolt driver.

What is gorm for hibernate?

The original implementation of GORM, GORM for Hibernate provides all the goodness of GORM for SQL databases. Built on the MongoDB Java Driver GORM for MongoDB includes features like Geospatial querying, Text search and Schemaless access etc. The GORM GraphQL library provides functionality to generate a GraphQL schema based on your GORM entities


2 Answers

Duplicate: Grails queries with criteria: how to get back a map with column?
And the corresponding answer (and solution): https://stackoverflow.com/a/16409512/1263227

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)
}     
like image 75
Diamond Hands Avatar answered Oct 09 '22 05:10

Diamond Hands


Agree with your question reasoning, this really should be part of the core GORM solution. That said, here's 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']]
like image 1
Josh Diehl Avatar answered Oct 09 '22 05:10

Josh Diehl