Hibernate criteria to sql.
Posted on: April 13, 2011 at 12:00 AM
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 CodeADS_TO_REPLACE_1

Related Tags for Hibernate criteria to sql.:

Advertisements

Ads

 
Advertisement null

Ads