class Publication {
String title
String author
Date datePublished
Integer numberOfPages
static namedQueries = {
recentPublications {
def now = new Date()
gt 'datePublished', now - 365
}
oldPublicationsLargerThan { pageCount ->
def now = new Date()
lt 'datePublished', now - 365
gt 'numberOfPages', pageCount
}
publicationsWithBookInTitle {
like 'title', '%Book%'
}
recentPublicationsWithBookInTitle {
// calls to other named queries...
recentPublications()
publicationsWithBookInTitle()
}
}
}
namedQueries
namedQueries 已不建議使用。請改用 where 查詢。
目的
namedQueries
靜態屬性定義命名查詢。命名查詢支援條件建立器語法。請參閱使用者指南中 條件建立器 章節以取得更多資訊。
範例
// get all recent publications...
def recentPubs = Publication.recentPublications.list()
// get up to 10 recent publications, skip the first 5...
def recentPubs = Publication.recentPublications.list(max: 10, offset: 5)
// get a single recent publication
def recentPub = Publication.recentPublications.get()
// get the number of recent publications...
def numberOfRecentPubs = Publication.recentPublications.count()
// get a recent publication with a specific id...
def pub = Publication.recentPublications.get(42)
// get all recent publications where title = 'Some Title'
def pubs = Publication.recentPublications.findAllWhere(title: 'Some Title')
// get a recent publication where title = 'Some Title'
def pub = Publication.recentPublications.findWhere(title: 'Some Title')
// dynamic finders are supported
def pubs = Publication.recentPublications.findAllByTitle('Some Title')
// get all old publications with more than 350 pages
def pubs = Publication.oldPublicationsLargerThan(350).list()
// get all old publications with more than 350 pages
// and the word 'Grails' in the title
def pubs = Publication.oldPublicationsLargerThan(350).findAllByTitleLike('%Grails%')
// get all recent publications with 'Book' in their title
def pubs = Publication.recentPublicationsWithBookInTitle().list()
命名查詢上的 list
方法支援與新增至網域類別的靜態 list
方法相同的屬性 (排序、順序、忽略大小寫、擷取等)。請參閱 list 文件以取得詳細資訊。
請注意,呼叫類似 Publication.recentPublications.get(42) 的內容與呼叫類似 Publication.get(42) 的內容不同。前者只會在 id 為 42 的 Publication 符合 recentPublications 命名查詢中定義的所有條件時,才會傳回 Publication 。
|
命名條件支援 listDistinct()
class PlantCategory {
Set plants
String name
static hasMany = [plants: Plant]
static namedQueries = {
withPlantsInPatch {
plants {
eq 'goesInPatch', true
}
}
}
}
class Plant {
boolean goesInPatch
String name
}
PlantCategory.withPlantsInPatch.listDistinct()
命名條件支援在呼叫時以條件封閉形式提供額外條件
// get all recent publications with author names beginning with Tony or Phil...
def books = Publication.recentPublications {
or {
like 'author', 'Tony%'
like 'author', 'Phil%'
}
}
// get the number of recent publications with
// author names beginning with Tony or Phil...
def numberOfBooks = Publication.recentPublications.count {
or {
like 'author', 'Tony%'
like 'author', 'Phil%'
}
}
命名條件可以串連在一起。當條件串連在一起時,查詢會產生為所有串連條件在單一條件封閉中組合的結果。
// recent publications with 'Book' in the title
def books = Publication.recentPublications.publicationsWithBookInTitle.list()
// old publications with more than 500 pages and with 'Book' in the title
def books = Publication.oldPublicationsLargerThan(500)
.publicationsWithBookInTitle
.list()
當命名查詢涉及網域類別關係,且關係類別定義命名查詢時,該命名查詢可以直接作為方法呼叫存取。範例
class Author {
static hasMany = [publications: Publication]
static namedQueries = {
authorsWithRecentPublications {
publications {
// invoking a named query defined in the Publication class...
recentPublications()
}
}
}
}
class Publication {
Author author
static namedQueries = {
recentPublications {
def now = new Date()
gt 'datePublished', now - 10
}
}
}