import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
@Repository
public class StudentDaoHibernateImpl extends HibernateDaoSupport implements StudentDao {
public Student getStudent(long studentId) thows StudentRetrieveException {
try {
//get hibernate session
Criteria crit = getSession().createCriteria(Student.class);
//join sub table
crit.setFetchMode("Subject", FetchMode.JOIN);
//filter student
crit.add(Restrictions.eq("studentId", studentId));
//create alias
crit.createAlias("Subject","sub");
//order by subject ID
crit.addOrder(Order.asc("sub.subjectId"));
//return result
return (Student) crit.uniqueResult();
} catch (HibernateException e) {
throw new StudentRetrieveException("Student Retrieve Problem",e);
}
}
}
No comments:
Post a Comment