Пример #1
0
  public <T> Criteria convertToCriteria(Class<T> c, Condition cnd) {

    Criteria cri = sessionFactory.getCurrentSession().createCriteria(c);

    for (Messenger msg : cnd.getMessages()) {
      if (msg.criterion != null) cri.add(msg.criterion);
      if (msg.order != null) cri.addOrder(msg.order);
    }

    return cri;
  }
Пример #2
0
  public <T> T fetch(Class<T> c, Condition cnd) {

    Criteria cri = sessionFactory.getCurrentSession().createCriteria(c);

    for (Messenger msg : cnd.getMessages()) {
      if (msg.criterion != null) cri.add(msg.criterion);
      if (msg.order != null) cri.addOrder(msg.order);
    }

    return (T) cri.uniqueResult();
  }
Пример #3
0
  /** calculate total number's record of table base on condition(s) */
  public <T> Number count(Class<T> c, Condition cnd) {

    Criteria cri = sessionFactory.getCurrentSession().createCriteria(c);

    if (cnd != null)
      for (Messenger msg : cnd.getMessages()) {
        if (msg.criterion != null) cri.add(msg.criterion);
        if (msg.order != null) cri.addOrder(msg.order);
      }

    return (Number) cri.setProjection(Projections.rowCount()).uniqueResult();
  }
Пример #4
0
  public <T> List<T> query(Class<T> c, Condition cnd, Page page) {

    Criteria cri = sessionFactory.getCurrentSession().createCriteria(c);

    if (cnd != null)
      for (Messenger msg : cnd.getMessages()) {
        if (msg.criterion != null) cri.add(msg.criterion);
        if (msg.order != null) cri.addOrder(msg.order);
        if (msg.max != null) cri.setMaxResults(msg.max);
      }

    if (page != null) {
      int limitFrom = page.getPageSize() * (page.getPageNumber() - 1);
      int limitTo = page.getPageSize() * page.getPageNumber();

      cri.setFirstResult(limitFrom);
      cri.setMaxResults(limitTo - limitFrom);
    }

    return cri.list();
  }
Пример #5
0
  public <T> T last(Class<T> c, Condition cnd) {

    Criteria cri = sessionFactory.getCurrentSession().createCriteria(c);

    if (cnd != null)
      for (Messenger msg : cnd.getMessages()) {
        if (msg.criterion != null) cri.add(msg.criterion);
        if (msg.order != null) cri.addOrder(msg.order);
      }

    cri.setMaxResults(1);

    for (Field f : c.getDeclaredFields()) {
      if (null != f.getAnnotation(Id.class)) {
        cri.addOrder(Order.desc(f.getName()));
        return (T) cri.uniqueResult();
      }
    }

    cri.addOrder(Order.desc("id"));

    return (T) cri.uniqueResult();
  }