Query Subset of Columns with Gorm
Suppose I have the following Domain class:
class Book {
String title
S开发者_开发百科tring 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
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]
...
}
In Grails (tested with 1.3.7 version) you can write:
def titlesAndAuthors = Book.withCriteria {
projections {
property 'title', 'title'
property 'author', 'author'
}
}
And you'll get a list of Object[] like above example.
精彩评论