JavaBeat
Hibernate Home Articles Resources Tutorials Hibernate Interview Questions & FAQs JSF QnA Code Junction

1. What is Hibernate?

2. Hibernate Architecture

3. Writing First Hibernate Code

4. Understanding Hibernate O/R Mapping

5. Understanding Hibernate <generator> element

6. Using Hibernate <generator> to generate id incrementally

7. Hibernate Query Language

8. Preparing table for HQL Examples

9. Writing ORM for Insurance table

10. HQL from clause Example

11. Hibernate Select Clause

12. HQL Where Clause Example

13. HQL Group By Clause Example

14. HQL Order By Example

15. Hibernate Criteria Query Example

16. Criteria Query Examples

17. Hibernate Native SQL Example

Hibernate Criteria Query Example


The Criteria interface allows to create and execute object-oriented queries. It is powerful alternative to the HQL but has own limitations. Criteria Query is used mostly in case of multi criteria search screens, where HQL is not very effective. 

The interface org.hibernate.Criteria is used to create the criterion for the search. The org.hibernate.Criteria interface represents a query against a persistent class. The Session is a factory for Criteria instances. Here is a simple example of Hibernate Criterial Query:


import org.hibernate.Session;
import org.hibernate.*;
import org.hibernate.cfg.*;
import java.util.*;

public class HibernateCriteriaQueryExample {
  public static void main(String[] args) {
    Session session = null;
    try {
      // This step will read hibernate.cfg.xml and prepare hibernate for
      // use
      SessionFactory sessionFactory = new Configuration().configure()
          .buildSessionFactory();
      session = sessionFactory.openSession();
      //Criteria Query Example
      Criteria crit = session.createCriteria(Insurance.class);
      List insurances = crit.list();
      for(Iterator it = insurances.iterator();it.hasNext();){
        Insurance insurance = (Insuranceit.next();
        System.out.println("ID: " + insurance.getLngInsuranceId());
        System.out.println("Name: " + insurance.getInsuranceName());
        
      }
      session.close();
    catch (Exception e) {
      System.out.println(e.getMessage());
    finally {
    }    
  }
}


The above Criteria Query example selects all the records from the table and displays on the console. In the above code the following code creates a new Criteria instance, for the class Insurance:

Criteria crit = session.createCriteria(Insurance.class);

The code:

List insurances = crit.list();

creates the sql query and execute against database to retrieve the data.




Sponsors
Webmaster Hosting Forum
Java Jobs
MyVideoLib
India News
Internet Advances
Latest QnA
Describe the lifecycle of a receiver application in order to receive a message?
Messages are not successful until they have been acknowledged. What are the types of acknowledgments?
What happens to messages if a transaction is rolled back?
What is the Role of the JMS Provider?
What is JMS administered object ?

JavaBeat Media (2004-2008), India
javabeat | planetoss | links directory | advertise
Copyright (2004 - 2008), JavaBeat