Showing posts with label alias to entity map. Show all posts
Showing posts with label alias to entity map. Show all posts

Tuesday, October 29, 2013

Grails/GORM createCriteria.list to get specific/selected columns only | Alias To Entity Map ALIAS_TO_ENTITY_MAP | Grails Projections | Projections Data

Grails/GORM createCriteria.list to get specific/selected columns only | Alias To Entity Map ALIAS_TO_ENTITY_MAP | Grails Projections | Projections Data.

Below is a example of get some specific field instead of all fields for domain.

And you can left join another domain that is belongsTo or hasMany of parent domain.


You have to import below reference:

import org.hibernate.criterion.CriteriaSpecification

List list = DomainName.createCriteria().list {
    resultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP)
    "in"("id", [1L, 2L])
    createAlias('x', 'x_alias', CriteriaSpecification.LEFT_JOIN)
    projections {
        property("id", "id")
        property("x_alias.field", "field_reference");
    }
}

Output would be like below:

[
    [id:1, field_reference:value1],
    [id:2, field_reference:value2]
]