namedQueries
Purpose
The namedQueries
static property is used to define named queries. Named queries support the criteria builder
syntax. See the section on the Criteria Builder in the user guide for more information.Examples
class Publication {
String title
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%'
}
}}
// 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 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%')
Note that calling something like Publication.recentPublications.get(42)
is not the same thing as
calling something like Publication.get(42)
. The former will only return a Publication
if the Publication
with id 42 meets all the criteria defined in the recentPublications
named query.