Home Hibernate Examples Criteria Hibernate criteria sqlRestriction.

Ask Questions?

View Latest Questions


 
 

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

Hibernate criteria sqlRestriction.

In this tutorial, we will introduce you to about the sqlRestriction() of Restrictions class. 

Syntax :

	Criteria criteria = session.createCriteria(StudentBean.class);
	criteria.add(Restrictions.sqlRestriction(" name like '%%'"));
	List lists = criteria.list();

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.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

public class CriteiaSort {
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(StudentBean.class);
		criteria.add(Restrictions.sqlRestriction(" name like '%%'"));
		List lists = criteria.list();
		Iterator obIter = lists.iterator();
		while (obIter.hasNext()) {
			StudentBean object = (StudentBean) obIter.next();
			System.out.print("Student Name : " + object.getName() + ",\t");
		}
	}
}
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_.class as class0_0_, this_.address as address0_0_, this_.email as email0_0_, this_.joindate as joindate0_0_, this_.fee as fee0_0_ from studentinfo this_ where name like '%%'

Student Name : bharat, Student Name : gyan, Student Name : vrishti, Student Name : parineeta, Student Name : Rohit,

Download Complete Source Code

Related Tags for Hibernate criteria sqlRestriction.:


DMCA.com