Home Hibernate Examples Criteria Hibernate criteria to sql.

Ask Questions?

View Latest Questions


 
 

Hibernate criteria to sql.
Posted on: April 13, 2011 at 12:00 AM
Advertisement
Hibernate criteria to sql.

Hibernate criteria to sql.

In this tutorial, you will see the use of sql() method of Expression class. The Expression class is available in org.hibernate.criteria package.

CriteiaSort.java

package net.roseindia.action;

import java.util.Iterator;
import java.util.List;
import net.roseindia.bean.StudentBean;
import net.roseindia.util.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;

@SuppressWarnings("deprecation")
public class CriteiaSort {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
	Session session = HibernateUtil.getSessionFactory().openSession();
	Criteria criteria = session.createCriteria(StudentBean.class).add(
			Expression.sql("lower({alias}.name) like lower(?)", "%r%",
					Hibernate.STRING));
	List obList = criteria.list();
	Iterator obIter = obList.iterator();
	while (obIter.hasNext()) {
		StudentBean object = (StudentBean) obIter.next();
		System.out.print(object.getName() + "\t");
		System.out.print(object.getAddress() + "\n");
	 }
    }
}
When you run this application it will display message as shown below:
Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.address as
address0_0_ from studentinfo this_ where lower(this_.name) like lower(?)
bharat     Bareilly
vrishti     Auraiya
parineeta   GKP
Rohit    Barabanki
Sarika  Jhashi

Download Complete Source Code

Related Tags for Hibernate criteria to sql.:


DMCA.com