Search code examples
grailshqlgrails-orm

How do I write a createCriteria in grails which pull only few columns from the table instead of all columns?


How do I write a createCriteria in grails which pull only few columns from the table instead of all columns?

I have a table called Ads. I want to retrieve only columns "Title" , "Price" and "Photo".

def c = Classified.createCriteria()
    def records = c.list {
            eq('publish_date', '2014-06-06')
        }
        maxResults(8)
    }

Above query retrieves all the records. How to restrict to only few columns?


Solution

  • Try this:

    def records = Classified.withCriteria {
        eq('publish_date', '2014-06-06')
    
        projections {
            property('title')
            property('price')
            property('photo')
        }        
        maxResults(8)
    }