Java Reference
In-Depth Information
Product.list() , which returns all product instances
Product.findByName(...) , which returns the first product matching the
name
Product.findAllByPriceGreaterThan(...) , which returns all the
products whose prices are greater than the argument
Product.findAllByNameIlikeAndPriceGreaterThan(...,...) ,
which returns products whose names satisfy a case-insensitive SQL like clause
and which have prices greater than the second argument
There are many more; see the Grails documentation [ 13 ] for details. In each case Grails uses
the mappings to generate SQL code satisfying the desired conditions.
13 See http://grails.org/doc/latest/ for the Grails documentation. Chapter 6 in those docs discusses GORM in detail.
Grails also uses Groovy to provide a builder for criteria queries. Hibernate has an API for
criteria queries that allows you to build up a query programmatically. The Java API works
but is still quite verbose. Grails dramatically simplifies it so that you can write expressions
like this:
Product.withCriteria {
like('name','%e%')
between('price', 2.50, 10.00)
order('price','desc')
maxResults(10)
}
This generates an SQL statement to find all products whose names include the letter e and
whose prices are between $2.50 and $10.00. It returns the first 10 matching products in
descending order by price.
One of the fundamental principles in Hibernate is the concept of a Hibernate session. As
stated in the previous section, Hibernate ensures that any object inside a Hibernate session
(what JPA calls a persistence context) will be kept in sync with the database. In Hibernate,
objects can be in one of three states, [ 14 ] as shown in figure 8.4 .
14 The Hibernate docs defining the states can be found at http://mng.bz/Q9Ry .
 
 
Search WWH ::




Custom Search