Can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Example On Hibernate Criteria Query

Hibernate » On Jul 12, 2011 | { 5 Comments }

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

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

Product.java

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;
	}
}

Product.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name="str.Product" table="products">

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

    </class>
</hibernate-mapping>

hibernate.cfg.xml

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

<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">jdbc:oracle:thin:@www.java4s.com:1521:XE</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="hbm2ddl.auto">update</property>

<mapping resource="Product.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>

ForOurLogic.java

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 { 

	@SuppressWarnings("unchecked")
	public static void main(String[] args)
	{

		Configuration cfg = new Configuration();
		cfg.configure("hibernate.cfg.xml"); 

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

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

		while(it.hasNext())
		{
			Product p=(Product)it.next();
			System.out.println(p.getProductId());
			System.out.println(p.getProName());
			System.out.println(p.getPrice());
			System.out.println("-----------------");
		}

		session.close();
		factory.close();
	}

}

Note:

  • 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.

 

What you are thinkig....

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

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

  2. Java4s says:

    @Mohit

    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 : java24.code@gmail.com

  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: u.balu99@gmail.com

If you want a pic to show with your comment, go get a gravatar !
Please post your questions on Java4s Answers forum

Name*
Ask a Question ?
or
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links Current & UpComing Tutorials Java4s.com
Tutorials Online :
spring Hibernate struts Json Ajax Log4j Log4j
coreJava Servlets


UpComing :
Servlets, Jsps
is optimized for learning java technologies, all the examples in this site are constantly reviewed to avoid errors. While using this site you agree to have read and accepted our terms of use and privacy policy
Especially i have prepared this blog by keeping fresher's in mind, however it will be very useful for real time developers too.


© 2013 Java4s All rights reserved. | strPro4Tut v(2.0) Theme designed by str-Graphics.com