In this example, you will see the use of Restrictions class in java. It is used to restrict the retrieval of data from database. Here, you will see the use of in constraint.
Syntax :
Criteria crit=session.createCriteria(model.class).add(Restrictions.in (propertyName, object[] values);
The in constraint has two argument one is property name and another is array of values of property. If the value of array is match to value of data base, then it returns result other wise not.
Structure of database Table....
CriteriaDAO.java
package roseindia.hibernateDAO; import java.util.Iterator; import java.util.List; import org.hibernate.criterion.*; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import roseindia.model.EmpInfo; import roseindia.model.StudentModel; public class CriteriaDAO { @SuppressWarnings("deprecation") public List<StudentModel> resultData(StudentModel obModel) { SessionFactory sessionFactory = new AnnotationConfiguration() .configure().buildSessionFactory(); Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(EmpInfo.class).add( Restrictions.in("fname", new String[] { "bharat", "ankit", "Vinay" })); List<StudentModel> studentList = criteria.list(); Iterator studentIterator = studentList.iterator(); return studentList; } } |
Advertisements
Ads
Ads