简体   繁体   中英

How do I create my query with Hibernate Criteria

I have to create the following query with Criteria:

SELECT r.*,( 
         SELECT COUNT(*) FROM APP.P_FORM_QUESTION_ANSWER qa 
         WHERE qa.J_STATUS = 'CORRECT' 
         AND qa.J_FORM_ID = r.J_FORM_ID AND qa.J_AUTHOR_ID = r.J_AUTHOR_ID 
         AND qa.J_FORM_RESULT_ID = r.J_ROW_ID
       ) as correctCount
FROM APP.P_FORM_RESULT r
WHERE r.J_FORM_ID = '123456'
ORDER BY correctCount DESC;

I try to use DetachedCriteria for extra column, but I do not see how to represent :

"qa.J_FORM_ID = r.J_FORM_ID AND qa.J_AUTHOR_ID = r.J_AUTHOR_ID 
 AND  qa.J_FORM_RESULT_ID = r.J_ROW_ID" 

in the DetachedCriteria, and add my DetachedCriteria as a new column

Criteria criteria =  
    HibernateUtil.getSession().createCriteria(FormResult.class, "formResult");
criteria.add(Restrictions.eq("formResult.formId", formId));

DetachedCriteria correctCount =  
    DetachedCriteria.forClass(QuestionAnswer.class, "questionAnswer");

correctCount.add(
      Restrictions.eq("questionAnswer.status",QuestionAnswerStatus.CORRECT));

// How to retrieve 'formResult' ?
correctCount.add(Restrictions.eq("questionAnswer.formId", "formResult.formId")); 

// How to retrieve 'formResult' ?
correctCount.add(Restrictions.eq("questionAnswer.authorId", "formResult.authorId"));  


 // How to retrieve 'formResult' ?
correctCount.add(
    Restrictions.eq("questionAnswer.formResult.rowId", "formResult.rowId"));
correctCount.setProjection(Projections.rowCount());

// How to add my DetachedCriteria as a new column
criteria.setProjection(Projections.alias(correctCount, "correctCount"));

The Lines with comments are the lines for which I cannot find the solution.

Finally, I created a different SQL query that gives me the same result:

SELECT r.J_ROW_ID, COUNT(DISTINCT qa.J_ROW_ID) as correctCount
FROM APP.P_FORM_RESULT r left join APP.P_FORM_QUESTION_ANSWER qa on qa.J_FORM_ID = r.J_FORM_ID AND qa.J_AUTHOR_ID = r.J_AUTHOR_ID AND qa.J_FORM_RESULT_ID = r.J_ROW_ID AND qa.J_STATUS = 'CORRECT'
WHERE r.J_FORM_ID = '123456'
GROUP BY r.J_ROW_ID
ORDER BY correctCount DESC;

And I used HQL because I cannot find how to do it in Criteria:

// HQL Query to retrieve the list of FormResult IDs
StringBuilder hql = new StringBuilder();
hql.append("SELECT r.id");
hql.append(" FROM QuestionAnswer qa RIGHT JOIN qa.formResult as r");
hql.append(" WHERE r.formId = :formId AND (qa.status = :status OR qa.status IS NULL)");
hql.append(" GROUP BY r.id");
hql.append(" ORDER BY COUNT(DISTINCT qa.id) DESC"));

Query query = HibernateUtil.getSession().createQuery(hql.toString());
query.setParameter("formId", formId);
query.setParameter("status", QuestionAnswerStatus.CORRECT);

// Retrieve the list of FormResult objects from the list of IDs previously retrieved
List<Long> result = query.list();
if (result != null && !result.isEmpty()) {
  for (Long resultId : result) {
    formResults.add(getData(FormResult.class, resultId));
  }
}

The downside is that I have to retrieve each FormResult one by one from my list of IDs retrieved by my HQL query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM