Magazine
 
Hibernate Query Language

Creating a Criteria instance:

To get a reference to the Criteria interface, use the createCriteria() method by invoking the Session class. This method takes the name of the ORM class on which the query has to be executed. In essence the Session acts as a factory for the Criteria class. The statement for
it would be written as:

Criteria criteria= session.createCriteria(Insurance.class)

The above statement returns a reference to Criteria for the Order ORM class.

Once created, you add Criterion objects (generally obtained from static methods of the Expression class) to build the query. Methods such as setFirstResult(), setMaxResults(), and setCacheable() may be used to customize the query behavior in the same way as in the
Query interface. Finally, to execute the query, the list() method is invoked.

For example:

crit.add(Expression.gt(“investementAmount”,
new Integer(900)));
List list = crit.list();

The above statements are fired on the investementAmount field of the Insurance class, which specify the list of those records that have the investment amount greater than 900.

Expressions:

The Hibernate Query API supports a rich set of comparison operators with the Expression class. The standard SQL operators (=, <, d”, >, e”) are supported by the following methods in the Expression class, respectively: eq(), lt(), le(), gt(), ge().

Following important methods of the Expression
class are shown in the table:

 
Method Description
Expression.between This is used to apply a “between” constraint to the named property
Expression.eq This is used to apply an “equal” constraint to the named property
Expression.ge This is used to apply a “greater than or equal” constraint to the named property
Expression.gt This is used to apply a “greater than”
constraint to the named property
Expression.lt This is used to apply a “less than” constraint to the named property
Expression.in This is used to apply an “in” constraint to the named property
Expression.le This is used to apply a “less than or equal” constraint to the named property
Expression.and This returns the conjunctions of two
expressions.
Expression.or This returns the disjunction of the two expressions.

2. Criterion

Another core API, in the relational approach conditions is know as criterion. A criterion is an instance of the interface org.hibernate.criterion.Criterion. To retrieve
data based on certain conditions, Restriction must be used on the criteria query. In other words, Criterion is the object-oriented representation of the “where” clause of a SQL query.

3. Restrictions:

The class org.hibernate.criterion.Restrictions defines
factory methods for obtaining certain built-in

Mar 2008 | Java Jazz Up | 46
 
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59,

Download PDF