////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////

GORM classes also support Hibernate's query language HQL, a very complete reference for which can be found https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#hql[in the Hibernate documentation] of the Hibernate documentation.

GORM provides a number of methods that work with HQL including link:../api/org/grails/datastore/gorm/GormEntity.html#find(java.lang.String)[find], link:../api/org/grails/datastore/gorm/GormEntity.html#findAll(java.lang.String)[findAll] and link:../api/org/grails/datastore/gorm/GormEntity.html#executeQuery(java.lang.String)[executeQuery].

An example of a query can be seen below:

[source,java]
----
def results =
      Book.findAll("from Book as b where b.title like 'Lord of the%'")
----


==== Named Parameters


In this case the value passed to the query is hard coded, however you can equally use named parameters:

[source,java]
----
def results =
      Book.findAll("from Book as b " +
                   "where b.title like :search or b.author like :search",
                   [search: "The Shi%"])
----

[source,java]
----
def author = Author.findByName("Stephen King")
def books = Book.findAll("from Book as book where book.author = :author",
                         [author: author])
----


==== Multiline Queries

Use the triple quoted strings to separate the query across multiple lines:

[source,java]
----
def results = Book.findAll("""
from Book as b,
     Author as a
where b.author = a and a.surname = :surname""", [surname:'Smith'])
----


==== Pagination and Sorting


You can also perform pagination and sorting whilst using HQL queries. To do so simply specify the pagination options as a Map at the end of the method call and include an "ORDER BY" clause in the HQL:

[source,java]
----
def results =
      Book.findAll("from Book as b where " +
                   "b.title like 'Lord of the%' " +
                   "order by b.title asc",
                   [max: 10, offset: 20])
----
