/** Use this inside subclasses as a convenience method. */
 @SuppressWarnings("unchecked")
 protected List<T> findByCriteria(Criterion... criterion) {
   Criteria crit = getSession().createCriteria(getEntityClass());
   for (Criterion c : criterion) {
     crit.add(c);
   }
   return crit.list();
 }
Пример #2
0
  private <P> Criteria buildProjectionCriteria(
      Class<P> projectClass, Criteria criteria, Projection projections, boolean distinctResult) {
    if (distinctResult) {
      criteria.setProjection(Projections.distinct(projections));
    } else {
      criteria.setProjection(projections);
    }

    return criteria.setResultTransformer(Transformers.aliasToBean(projectClass));
  }
Пример #3
0
 @Override
 public <T> List<T> findAll(Class<T> clazz, int firstResult, int maxResults, Order... orders) {
   Criteria criteria = getSession().createCriteria(clazz);
   if (orders == null || orders.length == 0) {
     return (List<T>) HibernateTool.setPaging(criteria, firstResult, maxResults).list();
   } else {
     criteria = HibernateTool.addOrders(criteria, orders);
     criteria = HibernateTool.setPaging(criteria, firstResult, maxResults);
     return (List<T>) criteria.list();
   }
 }
Пример #4
0
 public Teacher findByName(String name) {
   Criteria criteria;
   Transaction tx;
   Teacher teacher;
   Session session = ProductionSessionFactory.getDefault().getCurrentSession();
   tx = session.beginTransaction();
   try {
     criteria = session.createCriteria(persistentClass).add(Restrictions.eq("name", name));
     teacher = (Teacher) criteria.uniqueResult();
   } finally {
     tx.commit();
   }
   return teacher;
 }
  @SuppressWarnings("unchecked")
  @Override
  public List<Lancamento> listar(Conta conta, Date dataInicio, Date dataFim) {
    Criteria criteria = this.session.createCriteria(Lancamento.class);

    if (dataInicio != null && dataFim != null) {
      criteria.add(Restrictions.between("data", dataInicio, dataFim));
    } else if (dataInicio != null) {
      criteria.add(Restrictions.ge("data", dataInicio));
    } else if (dataFim != null) {
      criteria.add(Restrictions.le("data", dataFim));
    }

    criteria.add(Restrictions.eq("conta", conta));
    criteria.addOrder(Order.asc("data"));
    return criteria.list();
  }
Пример #6
0
  public Map<String, Object> lista(Map<String, Object> params) {
    log.debug("Buscando lista de facturas con params {}", params);
    if (params == null) {
      params = new HashMap<>();
    }

    if (!params.containsKey("max")) {
      params.put("max", 10);
    } else {
      params.put("max", Math.min((Integer) params.get("max"), 100));
    }

    if (params.containsKey("pagina")) {
      Long pagina = (Long) params.get("pagina");
      Long offset = (pagina - 1) * (Integer) params.get("max");
      params.put("offset", offset.intValue());
    }

    if (!params.containsKey("offset")) {
      params.put("offset", 0);
    }
    Criteria criteria = currentSession().createCriteria(FacturaAlmacen.class);
    Criteria countCriteria = currentSession().createCriteria(FacturaAlmacen.class);

    if (params.containsKey("almacen")) {
      criteria.createCriteria("almacen").add(Restrictions.idEq(params.get("almacen")));
      countCriteria.createCriteria("almacen").add(Restrictions.idEq(params.get("almacen")));
    }

    if (params.containsKey("filtro")) {
      String filtro = (String) params.get("filtro");
      Disjunction propiedades = Restrictions.disjunction();
      propiedades.add(Restrictions.ilike("folio", filtro, MatchMode.ANYWHERE));
      criteria.add(propiedades);
      countCriteria.add(propiedades);
    }

    if (params.containsKey("order")) {
      String campo = (String) params.get("order");
      if (params.get("sort").equals("desc")) {
        criteria.addOrder(Order.desc(campo));
      } else {
        criteria.addOrder(Order.asc(campo));
      }
    } else {
      criteria.createCriteria("estatus").addOrder(Order.asc("prioridad"));
    }
    criteria.addOrder(Order.desc("fechaModificacion"));

    if (!params.containsKey("reporte")) {
      criteria.setFirstResult((Integer) params.get("offset"));
      criteria.setMaxResults((Integer) params.get("max"));
    }
    params.put("facturas", criteria.list());

    countCriteria.setProjection(Projections.rowCount());
    params.put("cantidad", (Long) countCriteria.list().get(0));

    return params;
  }
Пример #7
0
 @Override
 public <P> List<P> reportList(
     Class<P> projectClass, ProjectionList projectionList, Criteria criteria) {
   Criteria report = buildProjectionCriteria(projectClass, criteria, projectionList, false);
   return (List<P>) report.uniqueResult();
 }
Пример #8
0
 @Override
 public <P> P reportOne(Class<P> projectClass, ProjectionList projectionList, Criteria criteria) {
   Criteria report = buildProjectionCriteria(projectClass, criteria, projectionList, true);
   return (P) report.uniqueResult();
 }
Пример #9
0
 @Override
 public long count(Criteria criteria) {
   return (Long) criteria.setProjection(Projections.rowCount()).uniqueResult();
 }
Пример #10
0
 @Override
 public <T> T findUnique(Class<T> clazz, Criteria criteria) {
   return (T) criteria.uniqueResult();
 }
Пример #11
0
 @Override
 public ScrollableResults scroll(Criteria criteria, ScrollMode scrollMode) {
   return criteria.scroll(scrollMode);
 }
Пример #12
0
 @Override
 public ScrollableResults scroll(Criteria criteria) {
   return criteria.scroll();
 }