Last updated by wangyou 8 years ago


Hibernate Criteria Builder

The Hibernate Criteria Builder allows you to create queries that map to the Hibernate Criteria API. There are equivalent builder nodes for most criterion within the Hibernate Expression class. For more info see the Builders Reference

The builder can be used standalone by passing a persistent class and sessionFactory instance:

new grails.orm.HibernateCriteriaBuilder(User.class, sessionFactory).list {
 		eq("firstName", "Fred")
Or an instance can be retrieved via the createCriteria method of Grails domain class instances:
def c = Account.createCriteria()
def results = c {
	like("holderFirstName", "Fred%")
	and {
		between("balance", 500, 1000)
		eq("branch", "London")
	order("holderLastName", "desc")
The results variable above cannot be typed as a List since it may actually be a proxy.

By default the builder returns a list of results, but can be forced to retrieve a unique result using the "get" node:

def c = Account.createCriteria()
def a = c.get {
	eq("number", 40830994)

Builders may contain control structures. The following example shows how to build a query that will match any item in a list.

def branchList = [ "London", "Newmarket", "Cambridge"]
def c = Account.createCriteria()
def results = c.list {
	or {
           for (b in branchList) {
               eq("branch", b)

OpenRico Builder

Grails provides support for the OpenRico project. See this document for more details on how OpenRico handles response content. Using OpenRicoBuilder in controllers users can generate any response for OpenRico requests, see the example below which uses the Google API to retrieve search results and then creates an OpenRico ajax response.

def result = google.doSearch();
   new grails.util.OpenRicoBuilder(response).ajax {
	object(id:"googleAutoComplete") {
	  for (re in result.resultElements) {
		div(class:"autoCompleteResult", re.URL)
It should be noted that at the moment, OpenRico requests are not handled by the Grails Ajax tags; there is no OpenRico implementation in JavascriptTagLib.