Hibernate Criteria, Adding Conditions To Criteria
If we want to add some sorting order for the objects, before the objects are going to store in list object then we need to add an Order class object to the Criteria class object by calling addOrder() method..,
- Order is a class given in “org.hibernate.Criterion” package
- In Order class, we have 2 static methods, asc()[ascending order] and dsc()[descending order] for getting an objects in required order
- Internal concept is, hibernate will select the records (rows) from PRODUCT table and stores them into a ResultSet and then converts each row data of resultset into a POJO class object basing on our field type, then all these objects into a list according to the order you have given
Let’s see an example..
required file…
- Product.java(POJO class)
- Product.hbm.xml
- hibernate.cfg.xml
- ForOurLogic.java (For writing our business logic)
Product.java
1234567891011121314151617181920212223242526272829303132333435package 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
1234567891011121314<?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
1234567891011121314151617181920<?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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546package 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.Order; 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);
crit.addOrder(Order.asc("price"));
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()); } session.close(); factory.close(); } }
Note:
- In line number 27, used Restrictions to get the price greater then 17000, and remember here price is the pojo class variable
- In line number 28, added criterion objects to criteria object
- line number 29 its our current program concept, we are calling addOrder() method to get the price in assenting order
- We can use for proName also, its depends on your requirement
post a comment