Home Hibernate Examples Criteria Hibernate criteria uniqueResult.

Ask Questions?

View Latest Questions


 
 

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

Hibernate criteria uniqueResult.

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.java

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

Download Complete Source Code

Related Tags for Hibernate criteria uniqueResult.:


DMCA.com