Here, we will introduce you to about the uniqueResult() method of Criteria class of hibernate. Hibernate Criteria class is available in org.hibernate.criteria package. The uniqueResult() method returns single instance of a persistence object. If no result found then method will return a null value.
Syntax :
Criteria criteria = session.createCriteria(StudentBean.class) .add(Restrictions.eq(propertyName,propertyValue)); modelClass obModel = (modelClass ) criteria.uniqueResult();)
CriteiaSort.javaADS_TO_REPLACE_1
package net.roseindia.action; import net.roseindia.bean.StudentBean; import net.roseindia.util.HibernateUtil; import org.hibernate.Criteria; import org.hibernate.Session; 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).add( Restrictions.eq("id", new Integer(3))); StudentBean stud = (StudentBean) criteria.uniqueResult(); System.out.println("Student = " + stud.getName()); } }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 this_.id=? Student = vrishti |
Advertisements
Ads
Ads