The document discusses various querying methods in Grails including dynamic finders, where queries, detached criteria, HQL queries, and performance optimizations. It provides examples of how to use findBy, findWhere, get, count, list and other dynamic finder methods. It also covers where queries, operators, aggregate functions, collections, subqueries, bulk updates/deletes, criteria queries, and HQL queries. It discusses returning different result types and filtering query results. Lastly, it mentions caching and performance techniques.
9. findBy Examples
// retrieve an album where the title contains 'Shake'
def album = Album.findByTitleLike('%Shake%')
// get an album created in last 10 days
def today = new Date()
def last10Days = Album.findByDateCreatedBetween(today-10,today)
// first album that is not 'Rock'
def somethingElse = Album.findByGenreNotEqual('Rock')
10.
11. findWhere
Todo t = Todo.findWhere([ "priority": "1", status: "ACTIVE"])
List<Todo> t = Todo.findAllWhere([ "priority": "1", status:
"ACTIVE"])
12. get
Todo t = Todo.get() //gets one record. if multiple exists then
throws error.
Todo t = Todo.get(1)
List<Todo> lt = Todo.getAll(1,3,5)
List<Todo> lt = Todo.getAll([1,3,5])
14. list
List<Todo> t = Todo.list()
List<Todo> t = Todo.listOrderByName()
List<Todo> t = Todo.list([max: 10, offset: 20, sort: "priority",
order: "desc"])
List<Todo> t = Todo.list(max: 10, offset: 20, sort: "priority",
order: "desc")
15. max - Specifies the maximum number of rows to return
offset - Specifies the number of elements into the ResultSet to
start at when returning values (useful for pagination)
sort - Specifies the field to sort on in the returned list
order - Specifies the order of the sort: "asc" or "desc" (default
is "asc")
ignoreCase - Sets sorting to ignore case (true by default)
fetch - Specifies eager/lazy fetch strategy as a Map of options
20. Operator Criteria Method Description
== eq Equal to
!= ne Not equal to
> gt Greater than
< lt Less than
>= ge Greater than or equal to
<= le Less than or equal to
in inList Contained within the given list
==~ like Like a given string
=~ ilike Case insensitive like
22. Aggregate functions
Method Description
avg The average of all values
sum The sum of all values
max The maximum value
min The minimum value
count The count of all values
property Retrieves a property of the resulting entities
23. functions
Method Description
second The second of a date property
minute The minute of a date property
hour The hour of a date property
day The day of the month of a date property
month The month of a date property
year The year of a date property
lower Converts a string property to upper case
upper Converts a string property to lower case
length The length of a string property
trim Trims a string property
29. find
Todo.find("From Todo as t
where t.name = :name
and t.priority = :priority
order by t.priority asc", [priority :"2", name : "Test"])
Todo.findAll("From Todo t where t.priority = :priority",
[priority:"1"],
[max: 10, offset: 20, sort: "priority", order "desc"])
30. find by example
Todo todo = new Todo()
todo.name = "Test"
todo = Todo.find(todo)
31. executeQuery
Todo.executeQuery("select t.name from Todo t where t.priority =
:priority ", [priority:"1"])
def users = User.executeQuery(
'from User u where u.username=:login or u.email=:login',
[login: ‘hd’])
32. Aggregate func
def newAuthors = Author.executeQuery(
"select a from Author a where a.books is empty")
def prolificAuthors = Author.executeQuery(
"select a from Author a where size(a.books) > 10")
def grailsAuthors = Author.executeQuery(
"select a from Author a join a.books as book " +
"where lower(book.title) like '%grails%'")
def author = Author.executeQuery(
"select a from Author a where :book in elements(a.books)",
[book: book])
33. Different return types
List<String> firstNames = Author.executeQuery('select a.firstName
from Author a')
List<Object[]> names = Author.executeQuery('select a.name, a.age
from Author a')
List<Map> names = Author.executeQuery(
'select new map(a.name as fullName, a.age as age) from Author
a where ...')
34. Return a POGO
List<Address> names = Author.executeQuery(
'select new Address(a.street, a.city, a.state, a.zip) from
Author a where ...')
class Author {
Author(String street, String city, String state, String zip) {
...
}
Author() {..}
}
35. filter map options
max - Limits the maximum number of records to return (typically
for pagination)
offset - Specifies the offset position into the results
(typically for pagination)
readOnly - If true, will return results that are not dirty-
checked
fetchSize - Specifies the fetch size for the underlying JDBC
query
timeout - Specifies the timeout for the underlying JDBC query
flushMode - Overrides the current session flush mode
cache - If true, will use the query cache