grailsgrails-2.3

Using result transformer in mongodb criteria


We're using MongoDB in our Grails 2.3.5 app without hibernate. Is there any way to use resultTransformer to convert property projection as Map like in hibernate.

For example:

  User.withCriteria {
        resultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP)
        projections {
            property('name', 'fullName')
        }
        def now = new Date()
        between('joinDate', now-365, now)
  }

This works in hibernate. Is this supported in Grails MongoDB or have some alternative to this.


Solution

  • No a ResultTransformer is a Hibernate specific API so cannot be used. You can write a general PostQuery event listener (see http://grails.github.io/grails-data-mapping/api/org/grails/datastore/mapping/query/event/PostQueryEvent.html) that can transform the results and register a listener (http://grails.github.io/grails-data-mapping/api/org/grails/datastore/mapping/engine/event/AbstractPersistenceEventListener.html)

    But this is a general approach and not really to be done a per query basis