Hibernate中Criteria的完整用法

Hibernate中Criteria的完整用法

一、查询对象

[java]  view plain  copy
  1. a、DetachedCriteria dc = DetachedCriteria.forClass(CredAnnexKhImage.class);  
  2. b、dc.add(Restrictions.eq("custCompanyInfo.id", custId));  
  3. c、dc.addOrder(Order.asc("parentType"));  
  4. d、执行:(Listthis.baseDao.getListForCriteria(dc);  

      补充:Restrictions用法

[java]  view plain  copy
  1. 1、Restrictions.like("reportDate", year + "%");  
  2. 2、Restrictions.or(可有多个条件用,分隔);  
  3. 3、Restrictions.in("checkResult"new String[]{"0""1""2"});  
  4. 4、Restrictions.isNull("checkResult")  
二、CallableCriteria

      1、用法:

[java]  view plain  copy
  1. a、CallableCriteria.getInstance(baseDao, cls).in("id", ids).addOrder(order).list();  
  2.    // baseDao:接口;cls:对象;ids:数组;order:参数;  
三、查询分页
[javascript]  view plain  copy
  1. public void queryPage(DetachedCriteria detachedCriteria, Page page) {  
  2.     Criteria criteria = detachedCriteria.getExecutableCriteria(getSession());  
  3.     Integer count = (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();  
  4.     if (null != count) {  
  5.         page.setTotalProperty(count);  
  6.     }  
  7.     LOGGER.debug("count:" + count);  
  8.     criteria.setProjection(null);  
  9.     criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);  
  10.     LOGGER.debug("PAGE PARAM start:" + page.getStart() + " limit:" + page.getLimit());  
  11.     page.setRoot(criteria.setFirstResult(page.getStart()).setMaxResults(page.getLimit()).list());  
  12. }  

你可能感兴趣的:(JAVA_WEB)