Example On Hibernate Criteria Query

Hibernate » on Jul 12, 2011 { 13 Comments } By Sivateja

Let us see an example program on hibernate criteria query,
files required….

  • class)
  • Product.hbm.xml
  • hibernate.cfg.xml
  • (For writing our business logic)

package str;

public class Product{

	private int productId;
	private String proName;
	private double price;

	public void setProductId(int productId)
	    this.productId = productId;
	public int getProductId()
	    return productId;

	public void setProName(String proName)
	    this.proName = proName;
	public String getProName()
	    return proName;

	public void setPrice(double price)
	    this.price = price;
	public double getPrice()
	    return price;


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

   <class name="str.Product" table="products">

<id name="productId" column="pid"  />
<property name="proName" column="pname" length="10"/>
<property name="price"/>



<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
<property name="connection.url"></property>
<property name="connection.username">system</property>
<property name="connection.password">admin</property>

<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="">update</property>

<mapping resource="Product.hbm.xml"></mapping>

package str;

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

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

public class ForOurLogic { 

	public static void main(String[] args)

		Configuration cfg = new Configuration();

		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();

		Criteria crit = session.createCriteria(Product.class);
		Criterion cn ="price",new Double(17000));
		List l=crit.list();
		System.out.println("List total size..._"+l.size());
		Iterator it=l.iterator();

			Product p=(Product);




  • In line number 25, created object of Criteria
  • In line number 26,  created Criterion interface object by using Restrictions class
  • In line number 27, added criterion interface object to criteria object
  • In line number 28,  executed criteria query by calling list() method in criteria
  • See line number 34, in this case we must typecast into our POJO class type only

Actually the internal concept is,  once we called the list() method in criteria (line number 28) all objects (records) will come and stores in list object, from there we used to take iterate then typecast into our POJO class type bla bla.


You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


13 Responses to “Example On Hibernate Criteria Query”
  1. Mohit says:

    Dear Java4s Team,
    Can U explain the importance of
    @SuppressWarnings(“unchecked”) in

  2. Java4s says:


    Its nothing..!
    Eclipse used to show some warnings right.., with @SuppressWarnings() we can inform to the eclipse not to show any warnings. This is optional.

  3. k.venkatarao says:

    this is venkat. i am a java developer.very much thanq’s to java4s team. this web site like class room discussion.
    please send struts-hibernate projects .

    my mail id is :

  4. kishore says:

    Nice article… keep posting….

  5. balu says:

    hai to java4s team
    this site is helping a lot for me
    your articles are very good
    please send struts examples
    my mail id is:

  6. Dilip says:

    Hi java4s,
    I Like This site very which is useful for freshers and experienced as well.
    In addition to the topics, Please add the advantages and disadvantages over the other things, performance etc., like, in this article why we should go for criteria other than HQL. which is good in performance. what to choose in a particular situation…

    Thanks in advance.

  7. Muthusamy says:

    This Java4s is very useful site for developers or java beginners. Having lot of information with detailed explanation.

    Thanks to java4s team! Keep up to add more contents.

  8. Honey Bee says:

    Criterion cn =“price”,new Double(17000));

    can you explain above line in more detail?

    what is gt()?

  9. Kalluri says:

    Hi Siva Teja,
    The content you posted in this site regarding hibernate is very nice tutorial.
    Please add annotations based concepts then that can be very useful for learners.


  10. Bharat says:

    What is the purpose of gt while creating criterion reference. can you explain. I think there should be criteria object and not gt().

  11. Kiran says:

    what is the difference between criteria and criterion in hibernate

  12. Shyam says:

    Hi Sivateja,

    Thank you for providing this article,
    Can you please explain, how to fetch data two different tables by using "Criteria API".
    Please give one example.


  13. AKib says:

    Use for loop instead of While loop please give me reply


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.