grails.orm
Class HibernateCriteriaBuilder

java.lang.Object
  extended by groovy.lang.GroovyObjectSupport
      extended by grails.orm.HibernateCriteriaBuilder
All Implemented Interfaces:
groovy.lang.GroovyObject

public class HibernateCriteriaBuilder
extends groovy.lang.GroovyObjectSupport

Wraps the Hibernate Criteria API in a builder. The builder can be retrieved through the "createCriteria()" dynamic static method of Grails domain classes (Example in Groovy):

                def c = Account.createCriteria()
                def results = c {
                        projections {
                                groupProperty("branch")
                        }
                        like("holderFirstName", "Fred%")
                        and {
                                between("balance", 500, 1000)
                                eq("branch", "London")
                        }
                        maxResults(10)
                        order("holderLastName", "desc")
                }
 

The builder can also be instantiated standalone with a SessionFactory and persistent Class instance:

         new HibernateCriteriaBuilder(clazz, sessionFactory).list {
                eq("firstName", "Fred")
         }
 

Since:
Oct 10, 2005
Author:
Graeme Rocher

Field Summary
static String AND
           
static String BETWEEN
           
static String EQUALS
           
static String EQUALS_PROPERTY
           
static String GREATER_THAN
           
static String GREATER_THAN_OR_EQUAL
           
static String GREATER_THAN_OR_EQUAL_PROPERTY
           
static String GREATER_THAN_PROPERTY
           
static String ID_EQUALS
           
static String ILIKE
           
static String IN
           
static String IS_EMPTY
           
static String IS_NOT_EMPTY
           
static String IS_NOT_NULL
           
static String IS_NULL
           
static String LESS_THAN
           
static String LESS_THAN_OR_EQUAL
           
static String LESS_THAN_OR_EQUAL_PROPERTY
           
static String LESS_THAN_PROPERTY
           
static String LIKE
           
static String NOT
           
static String NOT_EQUAL
           
static String NOT_EQUAL_PROPERTY
           
static String OR
           
static String ORDER_ASCENDING
           
static String ORDER_DESCENDING
           
static String RLIKE
           
static String SIZE_EQUALS
           
 
Constructor Summary
HibernateCriteriaBuilder(Class targetClass, org.hibernate.SessionFactory sessionFactory)
           
HibernateCriteriaBuilder(Class targetClass, org.hibernate.SessionFactory sessionFactory, boolean uniqueResult)
           
 
Method Summary
protected  void addProjectionToList(org.hibernate.criterion.Projection propertyProjection, String alias)
          Adds a projection to the projectList for the given alias
 void avg(String propertyName)
          Adds a projection that allows the criteria to return the property average value
 void avg(String propertyName, String alias)
          Adds a projection that allows the criteria to return the property average value
 Object between(String propertyName, Object lo, Object hi)
          Creates a "between" Criterion based on the property name and specified lo and hi values
 org.hibernate.Criteria buildCriteria(groovy.lang.Closure criteriaClosure)
           
 void cache(boolean shouldCache)
          Whether to use the query cache
 void count(String propertyName)
          Adds a projection that allows the criteria to return the property count
 void count(String propertyName, String alias)
          Adds a projection that allows the criteria to return the property count
 void countDistinct(String propertyName)
          Adds a projection that allows the criteria to return the distinct property count
 void countDistinct(String propertyName, String alias)
          Adds a projection that allows the criteria to return the distinct property count
 void distinct(Collection propertyNames)
          A distinct projection that takes a list
 void distinct(Collection propertyNames, String alias)
          A distinct projection that takes a list
 void distinct(String propertyName)
          A projection that selects a distince property name
 void distinct(String propertyName, String alias)
          A projection that selects a distince property name
 Object eq(String propertyName, Object propertyValue)
          Creates an "equals" Criterion based on the specified property name and value
 Object eqProperty(String propertyName, String otherPropertyName)
          Creates a Criterion that compares to class properties for equality
 void fetchMode(String associationPath, org.hibernate.FetchMode fetchMode)
          Sets the fetch mode of an associated path
 Object ge(String propertyName, Object propertyValue)
          Creates a "greater than or equal to" Criterion based on the specified property name and value
 Object geProperty(String propertyName, String otherPropertyName)
          Creates a Criterion that tests if the first property is greater than or equal to the second property
 org.hibernate.Criteria getInstance()
          Returns the criteria instance
 void groupProperty(String propertyName)
          Adds a projection that allows the criteria's result to be grouped by a property
 void groupProperty(String propertyName, String alias)
          Adds a projection that allows the criteria's result to be grouped by a property
 Object gt(String propertyName, Object propertyValue)
          Creates a "greater than" Criterion based on the specified property name and value
 Object gtProperty(String propertyName, String otherPropertyName)
          Creates a Criterion that tests if the first property is greater than the second property
 Object ilike(String propertyName, Object propertyValue)
          Creates a Criterion with from the specified property name and "ilike" (a case sensitive version of "like") expression
 Object in(String propertyName, Collection values)
          Applys a "in" contrain on the specified property
 Object in(String propertyName, Object[] values)
          Applys a "in" contrain on the specified property
 Object inList(String propertyName, Collection values)
          Delegates to in as in is a Groovy keyword
 Object inList(String propertyName, Object[] values)
          Delegates to in as in is a Groovy keyword
 Object invokeMethod(String name, Object obj)
           
 void join(String associationPath)
          Use a join query
 Object le(String propertyName, Object propertyValue)
          Creates a "less than or equal to" Criterion based on the specified property name and value
 Object leProperty(String propertyName, String otherPropertyName)
          Creates a Criterion that tests if the first property is less than or equal to the second property
 Object like(String propertyName, Object propertyValue)
          Creates a Criterion with from the specified property name and "like" expression
 void lock(boolean shouldLock)
          Whether a pessimisick lock should be obtained
 Object lt(String propertyName, Object propertyValue)
          Creates a "less than" Criterion based on the specified property name and value
 Object ltProperty(String propertyName, String otherPropertyName)
          Creates a Criterion that tests if the first property is less than the second property
 void max(String propertyName)
          Adds a projection that allows the criteria to retrieve a maximum property value
 void max(String propertyName, String alias)
          Adds a projection that allows the criteria to retrieve a maximum property value
 void min(String propertyName)
          Adds a projection that allows the criteria to retrieve a minimum property value
 void min(String propertyName, String alias)
          Adds a projection that allows the criteria to retrieve a minimum property value
 Object ne(String propertyName, Object propertyValue)
          Creates a "not equal" Criterion based on the specified property name and value
 Object neProperty(String propertyName, String otherPropertyName)
          Creates a Criterion that compares to class properties for !
 Object notEqual(String propertyName, Object propertyValue)
           
 Object order(String propertyName)
          Orders by the specified property name (defaults to ascending)
 Object order(String propertyName, String direction)
          Orders by the specified property name and direction
 void property(String propertyName)
          A projection that selects a property name
 void property(String propertyName, String alias)
          A projection that selects a property name
 void resultTransformer(org.hibernate.transform.ResultTransformer resultTransformer)
          Sets the resultTransformer.
 Object rlike(String propertyName, Object propertyValue)
          Creates a Criterion with from the specified property name and "rlike" (a regular expression version of "like") expression
 void rowCount()
          Adds a projection that allows the criteria to return the row count
 void rowCount(String alias)
          Adds a projection that allows the criteria to return the row count
 void select(String associationPath)
          Use a select query
 void setUniqueResult(boolean uniqueResult)
          Set whether a unique result should be returned
 Object sizeEq(String propertyName, int size)
          Creates a Criterion that contrains a collection property by size
 Object sizeGe(String propertyName, int size)
          Creates a Criterion that contrains a collection property to be greater than or equal to the given size
 Object sizeGt(String propertyName, int size)
          Creates a Criterion that contrains a collection property to be greater than the given size
 Object sizeLe(String propertyName, int size)
          Creates a Criterion that contrains a collection property to be less than or equal to the given size
 Object sizeLt(String propertyName, int size)
          Creates a Criterion that contrains a collection property to be less than to the given size
 Object sizeNe(String propertyName, int size)
          Creates a Criterion that contrains a collection property to be not equal to the given size
 Object sqlRestriction(String sqlRestriction)
          Applies a sql restriction to the results to allow something like: def results = Person.withCriteria { sqlRestriction "char_length( first_name ) <= 4" }
 void sum(String propertyName)
          Adds a projection that allows the criteria to retrieve the sum of the results of a property
 void sum(String propertyName, String alias)
          Adds a projection that allows the criteria to retrieve the sum of the results of a property
 
Methods inherited from class groovy.lang.GroovyObjectSupport
getMetaClass, getProperty, setMetaClass, setProperty
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

AND

public static final String AND
See Also:
Constant Field Values

IS_NULL

public static final String IS_NULL
See Also:
Constant Field Values

IS_NOT_NULL

public static final String IS_NOT_NULL
See Also:
Constant Field Values

NOT

public static final String NOT
See Also:
Constant Field Values

OR

public static final String OR
See Also:
Constant Field Values

ID_EQUALS

public static final String ID_EQUALS
See Also:
Constant Field Values

IS_EMPTY

public static final String IS_EMPTY
See Also:
Constant Field Values

IS_NOT_EMPTY

public static final String IS_NOT_EMPTY
See Also:
Constant Field Values

RLIKE

public static final String RLIKE
See Also:
Constant Field Values

BETWEEN

public static final String BETWEEN
See Also:
Constant Field Values

EQUALS

public static final String EQUALS
See Also:
Constant Field Values

EQUALS_PROPERTY

public static final String EQUALS_PROPERTY
See Also:
Constant Field Values

GREATER_THAN

public static final String GREATER_THAN
See Also:
Constant Field Values

GREATER_THAN_PROPERTY

public static final String GREATER_THAN_PROPERTY
See Also:
Constant Field Values

GREATER_THAN_OR_EQUAL

public static final String GREATER_THAN_OR_EQUAL
See Also:
Constant Field Values

GREATER_THAN_OR_EQUAL_PROPERTY

public static final String GREATER_THAN_OR_EQUAL_PROPERTY
See Also:
Constant Field Values

ILIKE

public static final String ILIKE
See Also:
Constant Field Values

IN

public static final String IN
See Also:
Constant Field Values

LESS_THAN

public static final String LESS_THAN
See Also:
Constant Field Values

LESS_THAN_PROPERTY

public static final String LESS_THAN_PROPERTY
See Also:
Constant Field Values

LESS_THAN_OR_EQUAL

public static final String LESS_THAN_OR_EQUAL
See Also:
Constant Field Values

LESS_THAN_OR_EQUAL_PROPERTY

public static final String LESS_THAN_OR_EQUAL_PROPERTY
See Also:
Constant Field Values

LIKE

public static final String LIKE
See Also:
Constant Field Values

NOT_EQUAL

public static final String NOT_EQUAL
See Also:
Constant Field Values

NOT_EQUAL_PROPERTY

public static final String NOT_EQUAL_PROPERTY
See Also:
Constant Field Values

SIZE_EQUALS

public static final String SIZE_EQUALS
See Also:
Constant Field Values

ORDER_DESCENDING

public static final String ORDER_DESCENDING
See Also:
Constant Field Values

ORDER_ASCENDING

public static final String ORDER_ASCENDING
See Also:
Constant Field Values
Constructor Detail

HibernateCriteriaBuilder

public HibernateCriteriaBuilder(Class targetClass,
                                org.hibernate.SessionFactory sessionFactory)

HibernateCriteriaBuilder

public HibernateCriteriaBuilder(Class targetClass,
                                org.hibernate.SessionFactory sessionFactory,
                                boolean uniqueResult)
Method Detail

getInstance

public org.hibernate.Criteria getInstance()
Returns the criteria instance

Returns:
The criteria instance

setUniqueResult

public void setUniqueResult(boolean uniqueResult)
Set whether a unique result should be returned

Parameters:
uniqueResult - True if a unique result should be returned

property

public void property(String propertyName)
A projection that selects a property name

Parameters:
propertyName - The name of the property

property

public void property(String propertyName,
                     String alias)
A projection that selects a property name

Parameters:
propertyName - The name of the property
alias - The alias to use

addProjectionToList

protected void addProjectionToList(org.hibernate.criterion.Projection propertyProjection,
                                   String alias)
Adds a projection to the projectList for the given alias

Parameters:
propertyProjection - The projection
alias - The alias

distinct

public void distinct(String propertyName)
A projection that selects a distince property name

Parameters:
propertyName - The property name

distinct

public void distinct(String propertyName,
                     String alias)
A projection that selects a distince property name

Parameters:
propertyName - The property name
alias - The alias to use

distinct

public void distinct(Collection propertyNames)
A distinct projection that takes a list

Parameters:
propertyNames - The list of distince property names

distinct

public void distinct(Collection propertyNames,
                     String alias)
A distinct projection that takes a list

Parameters:
propertyNames - The list of distince property names
alias - The alias to use

avg

public void avg(String propertyName)
Adds a projection that allows the criteria to return the property average value

Parameters:
propertyName - The name of the property

avg

public void avg(String propertyName,
                String alias)
Adds a projection that allows the criteria to return the property average value

Parameters:
propertyName - The name of the property
alias - The alias to use

join

public void join(String associationPath)
Use a join query

Parameters:
associationPath - The path of the association

lock

public void lock(boolean shouldLock)
Whether a pessimisick lock should be obtained

Parameters:
shouldLock - True if it should

select

public void select(String associationPath)
Use a select query

Parameters:
associationPath - The path of the association

cache

public void cache(boolean shouldCache)
Whether to use the query cache

Parameters:
shouldCache - True if the query should be cached

count

public void count(String propertyName)
Adds a projection that allows the criteria to return the property count

Parameters:
propertyName - The name of the property

count

public void count(String propertyName,
                  String alias)
Adds a projection that allows the criteria to return the property count

Parameters:
propertyName - The name of the property
alias - The alias to use

countDistinct

public void countDistinct(String propertyName)
Adds a projection that allows the criteria to return the distinct property count

Parameters:
propertyName - The name of the property

countDistinct

public void countDistinct(String propertyName,
                          String alias)
Adds a projection that allows the criteria to return the distinct property count

Parameters:
propertyName - The name of the property
alias - The alias to use

groupProperty

public void groupProperty(String propertyName)
Adds a projection that allows the criteria's result to be grouped by a property

Parameters:
propertyName - The name of the property

groupProperty

public void groupProperty(String propertyName,
                          String alias)
Adds a projection that allows the criteria's result to be grouped by a property

Parameters:
propertyName - The name of the property
alias - The alias to use

max

public void max(String propertyName)
Adds a projection that allows the criteria to retrieve a maximum property value

Parameters:
propertyName - The name of the property

max

public void max(String propertyName,
                String alias)
Adds a projection that allows the criteria to retrieve a maximum property value

Parameters:
propertyName - The name of the property
alias - The alias to use

min

public void min(String propertyName)
Adds a projection that allows the criteria to retrieve a minimum property value

Parameters:
propertyName - The name of the property

min

public void min(String propertyName,
                String alias)
Adds a projection that allows the criteria to retrieve a minimum property value

Parameters:
alias - The alias to use

rowCount

public void rowCount()
Adds a projection that allows the criteria to return the row count


rowCount

public void rowCount(String alias)
Adds a projection that allows the criteria to return the row count

Parameters:
alias - The alias to use

sum

public void sum(String propertyName)
Adds a projection that allows the criteria to retrieve the sum of the results of a property

Parameters:
propertyName - The name of the property

sum

public void sum(String propertyName,
                String alias)
Adds a projection that allows the criteria to retrieve the sum of the results of a property

Parameters:
propertyName - The name of the property
alias - The alias to use

fetchMode

public void fetchMode(String associationPath,
                      org.hibernate.FetchMode fetchMode)
Sets the fetch mode of an associated path

Parameters:
associationPath - The name of the associated path
fetchMode - The fetch mode to set

resultTransformer

public void resultTransformer(org.hibernate.transform.ResultTransformer resultTransformer)
Sets the resultTransformer.

Parameters:
resultTransformer - The result transformer to use.

eqProperty

public Object eqProperty(String propertyName,
                         String otherPropertyName)
Creates a Criterion that compares to class properties for equality

Parameters:
propertyName - The first property name
otherPropertyName - The second property name
Returns:
A Criterion instance

neProperty

public Object neProperty(String propertyName,
                         String otherPropertyName)
Creates a Criterion that compares to class properties for !equality

Parameters:
propertyName - The first property name
otherPropertyName - The second property name
Returns:
A Criterion instance

gtProperty

public Object gtProperty(String propertyName,
                         String otherPropertyName)
Creates a Criterion that tests if the first property is greater than the second property

Parameters:
propertyName - The first property name
otherPropertyName - The second property name
Returns:
A Criterion instance

geProperty

public Object geProperty(String propertyName,
                         String otherPropertyName)
Creates a Criterion that tests if the first property is greater than or equal to the second property

Parameters:
propertyName - The first property name
otherPropertyName - The second property name
Returns:
A Criterion instance

ltProperty

public Object ltProperty(String propertyName,
                         String otherPropertyName)
Creates a Criterion that tests if the first property is less than the second property

Parameters:
propertyName - The first property name
otherPropertyName - The second property name
Returns:
A Criterion instance

leProperty

public Object leProperty(String propertyName,
                         String otherPropertyName)
Creates a Criterion that tests if the first property is less than or equal to the second property

Parameters:
propertyName - The first property name
otherPropertyName - The second property name
Returns:
A Criterion instance

gt

public Object gt(String propertyName,
                 Object propertyValue)
Creates a "greater than" Criterion based on the specified property name and value

Parameters:
propertyName - The property name
propertyValue - The property value
Returns:
A Criterion instance

ge

public Object ge(String propertyName,
                 Object propertyValue)
Creates a "greater than or equal to" Criterion based on the specified property name and value

Parameters:
propertyName - The property name
propertyValue - The property value
Returns:
A Criterion instance

lt

public Object lt(String propertyName,
                 Object propertyValue)
Creates a "less than" Criterion based on the specified property name and value

Parameters:
propertyName - The property name
propertyValue - The property value
Returns:
A Criterion instance

le

public Object le(String propertyName,
                 Object propertyValue)
Creates a "less than or equal to" Criterion based on the specified property name and value

Parameters:
propertyName - The property name
propertyValue - The property value
Returns:
A Criterion instance

eq

public Object eq(String propertyName,
                 Object propertyValue)
Creates an "equals" Criterion based on the specified property name and value

Parameters:
propertyName - The property name
propertyValue - The property value
Returns:
A Criterion instance

sqlRestriction

public Object sqlRestriction(String sqlRestriction)
Applies a sql restriction to the results to allow something like:
       def results = Person.withCriteria {
           sqlRestriction "char_length( first_name ) <= 4"
       }
      

Parameters:
sqlRestriction - the sql restriction
Returns:
a Criterion instance

like

public Object like(String propertyName,
                   Object propertyValue)
Creates a Criterion with from the specified property name and "like" expression

Parameters:
propertyName - The property name
propertyValue - The like value
Returns:
A Criterion instance

rlike

public Object rlike(String propertyName,
                    Object propertyValue)
Creates a Criterion with from the specified property name and "rlike" (a regular expression version of "like") expression

Parameters:
propertyName - The property name
propertyValue - The ilike value
Returns:
A Criterion instance

ilike

public Object ilike(String propertyName,
                    Object propertyValue)
Creates a Criterion with from the specified property name and "ilike" (a case sensitive version of "like") expression

Parameters:
propertyName - The property name
propertyValue - The ilike value
Returns:
A Criterion instance

in

public Object in(String propertyName,
                 Collection values)
Applys a "in" contrain on the specified property

Parameters:
propertyName - The property name
values - A collection of values
Returns:
A Criterion instance

inList

public Object inList(String propertyName,
                     Collection values)
Delegates to in as in is a Groovy keyword


inList

public Object inList(String propertyName,
                     Object[] values)
Delegates to in as in is a Groovy keyword


in

public Object in(String propertyName,
                 Object[] values)
Applys a "in" contrain on the specified property

Parameters:
propertyName - The property name
values - A collection of values
Returns:
A Criterion instance

order

public Object order(String propertyName)
Orders by the specified property name (defaults to ascending)

Parameters:
propertyName - The property name to order by
Returns:
A Order instance

order

public Object order(String propertyName,
                    String direction)
Orders by the specified property name and direction

Parameters:
propertyName - The property name to order by
direction - Either "asc" for ascending or "desc" for descending
Returns:
A Order instance

sizeEq

public Object sizeEq(String propertyName,
                     int size)
Creates a Criterion that contrains a collection property by size

Parameters:
propertyName - The property name
size - The size to constrain by
Returns:
A Criterion instance

sizeGt

public Object sizeGt(String propertyName,
                     int size)
Creates a Criterion that contrains a collection property to be greater than the given size

Parameters:
propertyName - The property name
size - The size to constrain by
Returns:
A Criterion instance

sizeGe

public Object sizeGe(String propertyName,
                     int size)
Creates a Criterion that contrains a collection property to be greater than or equal to the given size

Parameters:
propertyName - The property name
size - The size to constrain by
Returns:
A Criterion instance

sizeLe

public Object sizeLe(String propertyName,
                     int size)
Creates a Criterion that contrains a collection property to be less than or equal to the given size

Parameters:
propertyName - The property name
size - The size to constrain by
Returns:
A Criterion instance

sizeLt

public Object sizeLt(String propertyName,
                     int size)
Creates a Criterion that contrains a collection property to be less than to the given size

Parameters:
propertyName - The property name
size - The size to constrain by
Returns:
A Criterion instance

sizeNe

public Object sizeNe(String propertyName,
                     int size)
Creates a Criterion that contrains a collection property to be not equal to the given size

Parameters:
propertyName - The property name
size - The size to constrain by
Returns:
A Criterion instance

ne

public Object ne(String propertyName,
                 Object propertyValue)
Creates a "not equal" Criterion based on the specified property name and value

Parameters:
propertyName - The property name
propertyValue - The property value
Returns:
The criterion object

notEqual

public Object notEqual(String propertyName,
                       Object propertyValue)

between

public Object between(String propertyName,
                      Object lo,
                      Object hi)
Creates a "between" Criterion based on the property name and specified lo and hi values

Parameters:
propertyName - The property name
lo - The low value
hi - The high value
Returns:
A Criterion instance

invokeMethod

public Object invokeMethod(String name,
                           Object obj)
Specified by:
invokeMethod in interface groovy.lang.GroovyObject
Overrides:
invokeMethod in class groovy.lang.GroovyObjectSupport

buildCriteria

public org.hibernate.Criteria buildCriteria(groovy.lang.Closure criteriaClosure)


Copyright (c) 2005-2009 The Grails project