Hibernate Criteria Queries

Hibernate Criteria Queries

Hibernate interface org.hibernate.Criteria can be use to query against chosen persistent class to pull data from database and session word as factory for Criteria object. Below is criteria query sample:

Note: This demo is extension of previous tutorial. If you are interested to see full working example with all set up please refer this tutorial Hibernate One To Many Annotation Example

  • Create Criteria instance to get all data from table:
Criteria criteria = session.createCriteria(Mother.class);
criteria.setMaxResults(25);
List<Mother> moList = criteria.list();
  • Narrow down result-set using condition:
        criteria = session.createCriteria(Mother.class);
        criteria.setMaxResults(25);
        criteria.add(Restrictions.like("firstName", "Mar%"));
        criteria.add(Restrictions.between("motherId", 1,1));
        moList = criteria.list();
  • Ordering results: Result can be ordered by using org.hibernate.criterion.Order
        criteria = session.createCriteria(Mother.class);        
        criteria.add(Restrictions.like("firstName", "Mar%"));
        criteria.addOrder( Order.asc("motherId") );
        criteria.setMaxResults(25);
        moList = criteria.list();     

 

  • Complete Java class:
package com.javahonk;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.javahonk.bean.Child;
import com.javahonk.bean.Mother;
import com.javahonk.util.HibernateUtil;

public class CriteriaTest {
    
    static SessionFactory sf = HibernateUtil.getSessionFactory();

    public static void main(String[] args) {
        
        Session session = sf.openSession();
        
        //********************************************************
        Criteria criteria = session.createCriteria(Mother.class);
        criteria.setMaxResults(25);
        List<Mother> moList = criteria.list();      
        
        for (Mother mother2 : moList) {
            System.out.println("Mother table data:");
            System.out.println("First Name: "+mother2.getFirstName()
                    +" Last Name: "+mother2.getLastName()+" City: "
                    +mother2.getCity()+" State: "+mother2.getState()
                    +" Zip: "+mother2.getZip()+"\n");
            System.out.println("Child table data:");
            Set<Child> childs=mother2.getChilds();
            for (Iterator<Child> iterator = childs.iterator(); 
                    iterator.hasNext();) {
                Child child3 = (Child) iterator.next();
                System.out.println("First Name: "
                +child3.getFirstName()+" Last name: "
                        +child3.getLastName()+"\n");
    
            }
    
        }
        
        //********************************************************
        criteria = session.createCriteria(Mother.class);
        criteria.setMaxResults(25);
        criteria.add(Restrictions.like("firstName", "Mar%"));
        criteria.add(Restrictions.between("motherId", 1,1));
        moList = criteria.list();  
        
        for (Mother mother2 : moList) {
            System.out.println("Mother table data:");
            System.out.println("First Name: "+mother2.getFirstName()
                    +" Last Name: "+mother2.getLastName()+" City: "
                    +mother2.getCity()+" State: "+mother2.getState()
                    +" Zip: "+mother2.getZip()+"\n");
            System.out.println("Child table data:");
            Set<Child> childs=mother2.getChilds();
            for (Iterator<Child> iterator = childs.iterator(); 
                    iterator.hasNext();) {
                Child child3 = (Child) iterator.next();
                System.out.println("First Name: "
                +child3.getFirstName()+" Last name: "
                        +child3.getLastName()+"\n");
    
            }
    
        }
        
        //********************************************************
        criteria = session.createCriteria(Mother.class);        
        criteria.add(Restrictions.like("firstName", "Mar%"));
        criteria.addOrder( Order.asc("motherId") );
        criteria.setMaxResults(25);
        moList = criteria.list();          
        
        for (Mother mother2 : moList) {
            System.out.println("Mother table data:");
            System.out.println("First Name: "+mother2.getFirstName()
                    +" Last Name: "+mother2.getLastName()+" City: "
                    +mother2.getCity()+" State: "+mother2.getState()
                    +" Zip: "+mother2.getZip()
                    +" Mother Id : "+mother2.getMotherId()+"\n");
            System.out.println("Child table data:");
            Set<Child> childs=mother2.getChilds();
            for (Iterator<Child> iterator = childs.iterator(); 
                    iterator.hasNext();) {
                Child child3 = (Child) iterator.next();
                System.out.println("First Name: "
                +child3.getFirstName()+" Last name: "
                        +child3.getLastName()+"\n");
    
            }
    
        }
        
        //********************************************************
        session.close();
        
    }

}

Output:

Hibernate Criteria Queries

For more information about HQL please read this hibernate official tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *