Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left join using hibernate criteria

I have two entity: Issue and Issue_Tracker. I am using Hibernate 3.6.

SELECT `issues`.`issue_id`,
       `issues`.`issue_raised_date`,
       `issues`.`issue_description`,
       `issue_tracker`.`tracker_status`
FROM `issues`
   LEFT JOIN  `issue_tracker` ON `issues`.`issue_id` = `issue_tracker`.`issue_id`
WHERE `issues`.`status`="Escalate To"

How to achieve this using Hibernate Criteria, and most Important, I have to use it for pagination.

and My Dao is as follows to show the list of Issues in jqgrid

public List showHelpDeskIssues(DetachedCriteria dc, int from, int size) {

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
 try
  {

    Criteria criteria = dc.getExecutableCriteria(session);
    criteria.setFirstResult(from);
    criteria.setMaxResults(size);
    criteria.add(Restrictions.eq("status","Escalate To"));

    return criteria.list();
  }
  catch (HibernateException e)
  {
    e.printStackTrace();
    throw e;
  } }

For brief explanation please refer this question how to show two tables data in jqgrid using struts2 - jqgrid plugin and hibernate any help would be great.

like image 392
arvin_codeHunk Avatar asked Jan 08 '13 12:01

arvin_codeHunk


2 Answers

you can try the following

Criteria criteria = session.createCriteria(Issues.class);
criteria.setFirstResult(from);
criteria.setMaxResults(size);
criteria.setFetchMode('parent.child', FetchMode.JOIN);
criteria.add(Restrictions.eq("status", "Escalate To");
List<Issues> list= criteria.list();

here parent is the property name in Issues.java and child is the property in IssueTracker.java.

like image 129
Anantha Sharma Avatar answered Sep 19 '22 23:09

Anantha Sharma


Well,

follow one sample...

Criteria crit = session.createCriteria(Issues.class);
crit.createAlias("otherClass", "otherClass");
crit.add(Restrictions.eq("otherClass.status", "Escalate To"));
List result = crit.list();

I think so this can to help!!

like image 35
Altieres de Matos Avatar answered Sep 20 '22 23:09

Altieres de Matos