Search code examples
hibernategrailsgrails-ormhibernate-criteria

Query Subset of Columns with Gorm


Suppose I have the following Domain class:

class Book {
  String title
  String author
  byte[] largeCoverArtImage
}

I have a list view where I do not need to display largeCoverArtImage, how can I perform the following SQL query using GORM Criteria?

select title, author from Book

Solution

  • You can run HQL queries that select individual columns with executeQuery:

    def titlesAndAuthors = Book.executeQuery('select title, author from Book')
    

    This will return a List of Object[], e.g.

    for (row in titlesAndAuthors) {
       String title = row[0]
       String author = row[1]
       ...
    }