Hibernate Criteria uniqueResult

时间:2015-05-21 21:15:27

标签: java sql hibernate criteria

我的SQL查询有点像下面的

SELECT count(1),tran_nbr FROM AAA WHERE P_D IN (3,4) AND
 DTE_OF_ISS BETWEEN to_date('01/01/2014','mm/dd/yyyy') and to_date('01/31/2015' ,'mm/dd/yyyy')

我已经编写了这样的hibernate查询。

Criteria criteria = session.createCriteria(AAA.class).setProjection(Projections.rowCount());


   criteria.add(Restrictions.sqlRestriction("(P_D IN ('4'))"));

   if (reportsForm.getFromDate() != null && reportsForm.getToDate() != null) {
        criteria.add(Restrictions.sqlRestriction("DTE_OF_ISS = to_date('"+ utils.convertDateToString(reportsForm.getFromDate()) + "', 'MM/dd/yyyy')"));
        criteria.add(Restrictions.sqlRestriction("DTE_OF_ISS <= to_date('"+ utils.convertDateToString(reportsForm.getToDate()) + "', 'MM/dd/yyyy')"));
    }

  criteria.setFetchSize(100);
criteria.setMaxResults(500);
    listResult = criteria.list();

   listResult = criteria.list();
    int count = ((Long) criteria.uniqueResult()).intValue();

当我看到“show_sql”时,查询看起来像这样。

select * from ( select count(*) as y0_ from Schema.AAA this_ where (P_D IN ('4')) and DTE_OF_ISS = to_date('01/01/2014', 'MM/dd/yyyy') and DTE_OF_ISS <= to_date('01/31/2015', 'MM/dd/yyyy') ) where rownum <= ?

不知道添加选择来自哪里和rownum?请帮帮我......

0 个答案:

没有答案