(Quick Reference)

executeQuery

Purpose

Allows the execution of HQL queries against a domain class

Examples

// simple query
Account.executeQuery( "select distinct a.number from Account a" );
// using with list of parameters
Account.executeQuery( "select distinct a.number from Account a where a.branch = ? and a.created > ?", ['London',lastMonth] );
// using with a single parameter and pagination params (since 0.5)
Account.executeQuery( "select distinct a.number from Account a where a.branch = ?", ['London'], [max:10,offset:5] );
// using with Map of named parameters (since 0.5)
Account.executeQuery( "select distinct a.number from Account a where a.branch = :branch", [branch:'London'] );
// using with Map of named parameters and pagination params (since 0.5)
Account.executeQuery( "select distinct a.number from Account a where a.branch = :branch", [branch:'London', max:10, offset:5] );
// same as previous
Account.executeQuery( "select distinct a.number from Account a where a.branch = :branch", [branch:'London'], [max:10, offset:5] );

Description

The executeQuery method allows the execution of arbitrary HQL queries that don't necessary return instances of a domain class. This is useful if you only want a subset of data instead of the whole object hierarchy. The basic syntax is:

Book.executeQuery( String query )
Book.executeQuery( String query, Collection positionalParams )
Book.executeQuery( String query, Collection positionalParams, Map paginateParams )
Book.executeQuery( String query, Map namedParams )
Book.executeQuery( String query, Map namedParams, Map paginateParams )

Parameters:

  • query - An HQL query
  • positionalParams - A List of parameters for a positional parametrized HQL query
  • namedParams - A Map of named parameters a HQL query
  • paginateParams - A Map containing parameters 'max' or/and 'offset' (see examples below)