7

假设我有以下 Domain 类:

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

我有一个不需要显示的列表视图largeCoverArtImage,如何使用 GORM Criteria 执行以下 SQL 查询?

select title, author from Book
4

2 回答 2

7

在 Grails(用 1.3.7 版本测试)中,您可以编写:

def titlesAndAuthors = Book.withCriteria {
        projections {
            property 'title', 'title'
            property 'author', 'author'
        }
}

你会得到一个像上面例子一样的 Object[] 列表。

于 2011-05-17T13:34:53.417 回答
7

您可以使用 executeQuery 运行选择单个列的 HQL 查询:

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

这将返回一个 Object[] 列表,例如

for (row in titlesAndAuthors) {
   String title = row[0]
   String author = row[1]
   ...
}
于 2010-07-09T03:59:12.980 回答