Sunday, January 25, 2009

Hibernate Criteria API



Download PDF: http://www.javapassion.com/j2ee/hibernatecriteria.pdf



Hibernate Criteria API


Three ways of retrieving data in

Hibernate

● Criteria query API

The easiest way to retrieve data Pure Java language based

● Hibernate Query Language (HQL)

● Native SQL query


How to use Criteria Query API

● Create org.hibernate.Criteria object via

createCriteria() factory method of the Session

– Pass persistent object's class or its entity name to

the createCriteria() method

● Call list() method of the Criteria object

// Get all instances of Person class and its subclasses

Criteria crit = sess.createCriteria(Person.class);

List results = crit.list();


Criteria Query API Feature

● Uses a set of Java objects for constructing queries

– Instead of query language

● Lets you build nested, structured query expressions

in Java programming language

– Compile time syntax checking possible

– Polymorphic behavior – get instances of X & subclass(X)

● Supports Query By Example (QBE)

– Performing a query by providing an example object that

contain properties that need to be retrieved

● Supports aggregation methods (from Hibernate 3)

– Count


No comments: