Left join using hibernate criteria

arvin_codeHunk picture arvin_codeHunk · Jan 8, 2013 · Viewed 35.9k times · Source

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.

Answer

Anantha Sharma picture Anantha Sharma · Jan 8, 2013

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.