3

In rails,we can use Order.find(:all,:select=>"id,name...... or Order.where(....).select("id,name") to limit the column.

But i can not find the similar way in Grails. so can you give me some help for it? thanks.

2 Answers2

3

There aren't any possible parameters for the Grails dynamic finders to limit the properties returned by the finder. The best alternative I know of is to use projections within a criteria, i.e.

Book.withCriteria {
    like 'author', 'Will%'
    projections {
        property 'title'
        property 'author'
    }
}

For more, see the criteria reference.

doelleri
  • 19,232
  • 5
  • 61
  • 65
  • I don't understand your question – doelleri Mar 27 '12 at 05:34
  • +1, but a query with projections only returns a list of properties instead of domain instances, therefore you can not further update the properties with what returned from it. – coderLMN Mar 08 '13 at 03:07
1

You can't limit the columns returned when using a dynamic finder like findBy. Intead you must use either a criteria query with projections, or a HQL query executed with executeQuery

Community
  • 1
  • 1
Dónal
  • 185,044
  • 174
  • 569
  • 824