Wednesday, January 11, 2012

Criteria in Hibernate


Criteria API
=========
 Hibernate Criteria API is a more object oriented and elegant alternative to Hibernate Query Language (HQL).

 Criteria criteria = session.createCriteria(Stock.class);
List<Stock> records = crit.list();

This will query the stock table and retrieves all the records in stock table.

Adding Restrictions
===================
Restrictions are same as adding where clause in sql. The available restrictions are:

Method
Parameters
Purpose
allEq
Mapproperties
A shortcut for requiring several properties to have particular values. The keys of the supplied map are the names of the properties you want to constrain, while the values in the map are the target values each property must equal if an entity is to be included in the query results. The returned Criterion ensures that each named propertyhas the corresponding value.
and
Criterionlhs,Criterionrhs
Builds a compound Criterion that requires both halves to be met in order for the whole to succeed. See also conjunction⁠⁠(⁠⁠).
between
Stringproperty,Object low,Objecthigh
Requires the value of the named property to fall between the values of low and high.
conjunction
None
Creates a Conjunction object which can be used to build an “and” criterion with as many pieces as you need. Simply call itsadd⁠⁠(⁠⁠) method with each of the Criterion instances you want to check. The conjunction will be true if and only if all its component criteria are true. This is more convenient than building a tree of and⁠⁠(⁠⁠) criteria “by hand.” The add⁠⁠(⁠⁠) method of the Criteria interface acts as though it contains a Conjunction.
disjunction
None
Creates a Disjunction object that can be used to build an “or” criterion with as many pieces as you need. Simply call itsadd⁠⁠(⁠⁠) method with each of the Criterion instances you want to check. The disjunction will be true if any of its component criteria are true. This is more convenient than building a tree ofor⁠⁠(⁠⁠) criteria “by hand.” See Example 8.10.
eq
Stringproperty,Objectvalue
Requires the named property to have the specified value.
eqProperty
Stringproperty1,Stringproperty2
Requires the two named properties to have the same value.
ge
Stringproperty,Objectvalue
Requires the named property to be greater than or equal to the specified value.
geProperty
Stringproperty1,Stringproperty2
Requires the first named property to be greater than or equal to the second.
gt
Stringproperty,Objectvalue
Requires the named property to be greater than the specified value.
gtProperty
Stringproperty1,Stringproperty2
Requires the first named property to be greater than the second.
idEq
Objectvalue
Requires the identifier property to be equal to the specified value.
ilike
Stringproperty,Objectvalue
A case-insensitive “like” operator. See like.
ilike
Stringproperty,Stringvalue,MatchModemode
A case-insensitive “like” operator for people who don’t want to mess with “like” syntax and just want to match a substring. MatchMode is a type-safe enumeration with values START, END, ANYWHERE, andEXACT. This method adjusts the syntax of value to reflect the kind of matching specified by mode, then proceeds like the two-parameter ilike⁠⁠(⁠⁠).
in
Stringproperty,Collectionvalues
A shortcut for allowing the named property to have any of the values contained in the collection. This is more convenient than building up a disjunction⁠⁠(⁠⁠) of eq⁠⁠(⁠⁠) criteria “by hand.”
in
Stringproperty,Object[]values
A shortcut for allowing the named property to have any of the values contained in the array. This is more convenient than building up adisjunction⁠⁠(⁠⁠) of eq⁠⁠(⁠⁠) criteria “by hand.”
isEmpty
Stringproperty
Requires the named collection property to be empty (have zero length).
isNotEmpty
Stringproperty
Requires the named collection property to be have one or moreelements.
isNotNull
Stringproperty
Requires the named property to have a value other than null.
isNull
Stringproperty
Requires the named property to be null.
le
Stringproperty,Objectvalue
Requires the named property to be less than or equal to the specified value. See Example 8.3.
leProperty
Stringproperty1,Stringproperty2
Requires the first named property to be less than or equal to the second.
like
Stringproperty,Objectvalue
Requires the named property to be “like” the specified value (in the sense of the SQL like operator, which allows simple substring matching). See Examples 8.8 and 8.16.
like
Stringproperty,Stringvalue,MatchModemode
A “like” operator for people who don’t want to mess with “like” syntax and just want to match a substring. See ilike⁠⁠(⁠⁠) for more details.
lt
Stringproperty,Objectvalue
Requires the named property to be less than the specified value.
ltProperty
Stringproperty1,Stringproperty2
Requires the first named property to be less than the second.
naturalId
None
Allows selection through a multicolumn “natural business key”mapped using natural-id.
ne
Stringproperty,Objectvalue
Requires the named property to have any value other than the specified value.
neProperty
Stringproperty1,Stringproperty2
Requires the two named properties to have different values.
not
Criterionexpression
Negates the supplied Criterion (if it matched, this one fails, and vice versa).
or
Criterionlhs,Criterionrhs
Builds a compound Criterion that succeeds if either of its halves matches. See also disjunction⁠⁠(⁠⁠).
sizeEq
Stringproperty,int size
Requires the named collection property to have the specified number of elements.
sizeGe
Stringproperty,int size
Requires the named collection property to have at least the specified number of elements.
sizeGt
Stringproperty,int size
Requires the named collection property to have more than the specified number of elements.
sizeLe
Stringproperty,int size
Requires the named collection property to have no more than the specified number of elements.
sizeLt
Stringproperty,int size
Requires the named collection property to have fewer than the specified number of elements.
sizeNe
Stringproperty,int size
Requires the named collection property to have a different number of elements than size.
sqlRestriction
String sql
Applies a constraint expressed in the native SQL dialect of the underlying database system. This can be very powerful, but be aware it might lead to loss of portability.
sqlRestriction
String sql,Object[]values,Type[]types
Applies a constraint expressed in the native SQL of the underlying database, with the supplied JDBC parameters. This can be very powerful, but be aware it might lead to loss of portability.
sqlRestriction
String sql,Objectvalue, Typetype
Applies a constraint expressed in the native SQL of the underlying database, with the supplied JDBC parameter. This can be very powerful, but be aware it might lead to loss of portability.

Sample codes using restrictions
=============================== 

//To display all the details of Stock name includes pad
 Criteria crit = session.createCriteria(Stock.class);
 crit.add( Restrictions.like("stockName", "pad%") );
 List<Stock> records = crit.list();
 //To display all the details of Stock one AND and two OR operations

Criteria crit1 = session.createCriteria(Stock.class);
crit1.add( Restrictions.like("stockName", "pad%") ).
add(Restrictions.or(Restrictions.eq("stockCode", "7052"),
Restrictions.eq("stockCode", "70588")));
List<Stock> records1 = crit1.list();

           
//To display all the details of Stock on THREE OR operations
       
Criteria crit2 = session.createCriteria(Stock.class);
crit2.add(Restrictions.or(Restrictions.like("stockName", "pad%")
Restrictions.or(Restrictions.eq("stockCode", "70531"),
Restrictions.eq("stockCode", "70588"))));
List<Stock> records2 = crit2.list(); 

No comments: