In the latest documentation of Grails, we can read:
Querying Associations. Associations can also be used within queries:
def author = Author.findByName("Stephen King")
def books = author ? Book.findAllByAuthor(author) : []
I'd like to know what is the meaning of ?
and : []
Shorthand if
statement in Groovy
(and Java
, see the first comment).
def books = author ? Book.findAllByAuthor(author) : []
is equivalent of:
def books
if (author) {
books = Book.findAllByAuthor(author)
}
else {
books = []
}
See the elvis operator
(Groovy
only, not Java
) here.