(快速參考)

findAll

用途

找出符合指定查詢的所有網域類別實例

範例

// everything
Book.findAll()

// with a positional parameter
Book.findAll("from Book as b where b.author=?", ['Dan Brown'])

// 10 books from Dan Brown staring from 5th book ordered by release date
Book.findAll("from Book as b where b.author=? order by b.releaseDate",
             ['Dan Brown'], [max: 10, offset: 5])

// examples with max/offset usage
def query = "from Book as b where b.author='Dan Brown' order by b.releaseDate"

// first 10 books
Book.findAll(query, [max: 10])

// 10 books starting from 5th
Book.findAll(query, [max: 10, offset: 5])

// examples with named parameters
Book.findAll("from Book as b where b.author=:author",
             [author: 'Dan Brown'])
Book.findAll("from Book as b where b.author=:author",
             [author: 'Dan Brown'], [max: 10, offset: 5])
Book.findAll("from Book as b where b.author in (:authors)",
             [authors: ['Dan Brown', 'Jack London']])

// examples with tables join
Book.findAll("from Book as b where not exists " +
                "(from Borrow as br where br.book = b)")

// query by example
def example = new Book(author: "Dan Brown")
Book.findAll(example)

// Use where criteria (since Grails 2.0)
def results = Person.findAll {
     lastName == "Simpson"
}
def results = Person.findAll(sort:"firstName") {
     lastName == "Simpson"
}

說明

findAll 方法允許使用 Hibernate 的查詢語言 HQL 進行查詢,並透過範例查詢,傳回所有符合的實例。分頁可以使用 maxoffset 參數控制

Book.findAll("from Book as b where b.author=:author",
             [author: 'Dan Brown'], [max: 10, offset: 5])

findAll 方法支援第 2 層快取

Book.findAll("from Book as b where b.author=:author",
             [author:'Dan Brown'], [max: 10, offset: 5, cache: true])

此方法的基本語法為

Book.findAll()
Book.findAll(String query)
Book.findAll(String query, Collection positionalParams)
Book.findAll(String query, Collection positionalParams, Map queryParams)
Book.findAll(String query, Map namedParams)
Book.findAll(String query, Map namedParams, Map queryParams)
Book.findAll(Book example)
Book.findAll(Closure whereCriteria)
Book.findAll(Map queryParams, Closure whereCriteria)
如果您的 HQL 中有聯結,此方法將傳回包含網域類別實例的 `Array` 的 List。在此情況下,您可能希望使用 executeQuery,以便更靈活地指定要傳回的內容。

參數

  • query - HQL 查詢

  • positionalParams - 位置參數化 HQL 查詢的 List

  • namedParams - HQL 查詢的名稱參數的 Map

  • queryParams - 包含參數 'max' 和/或 'offset' 和/或 'cache' 的 Map

  • example - 範例查詢的網域類別實例

  • readOnly - 如果傳回的物件不應自動進行髒污檢查,則為 true(類似於 read()

  • fetchSize - 基礎 JDBC 驅動程式每次往返擷取的列數

  • flushMode - Hibernate FlushMode 覆寫,預設為 FlushMode.AUTO

  • timeout - 查詢逾時(秒)