Example usage for org.hibernate Criteria addOrder

List of usage examples for org.hibernate Criteria addOrder

Introduction

In this page you can find the example usage for org.hibernate Criteria addOrder.

Prototype

public Criteria addOrder(Order order);

Source Link

Document

Add an Order ordering to the result set.

Usage

From source file:ca.qc.cegepoutaouais.tge.pige.server.ManagementServiceImpl.java

License:Open Source License

@Override
public List<Category> getCategories(Category parent, Boolean includeUnclassified) throws PigeException {

    logger.debug("Rcupration des catgories...");

    Transaction tx = null;//  w w w .  j  a v  a 2  s.  c  om
    List<Category> categories = null;
    Session session = null;

    try {
        session = PigeHibernateUtil.openSession();
        tx = session.beginTransaction();

        Criteria criteria = session.createCriteria(Category.class);
        if (parent != null) {
            logger.debug("category != null: " + parent.getName());
            criteria.add(Restrictions.eq(Category.PARENT_REF, parent));
        } else {
            criteria.add(Restrictions.isNull(Category.PARENT_REF));
        }
        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

        categories = (List) criteria.addOrder(Order.asc(Category.NAME_REF)).list();

        if (categories != null) {
            for (Category c : categories) {
                Criteria itemCrit = session.createCriteria(Item.class);
                itemCrit.createCriteria(Item.CATEGORIES_REF)
                        .add(Restrictions.like(Category.PATH_REF, c.getPath(), MatchMode.START));
                itemCrit.setProjection(Projections.distinct(Projections.rowCount()));
                c.setItemCount((Integer) itemCrit.uniqueResult());
            }
        }

        //  la racine seulement.
        if (includeUnclassified && parent == null) {
            Category unclassified = new Category();
            unclassified.setId(Category.UNCLASSIFIED_CATEGORY_ID);
            Criteria itemCrit = session.createCriteria(Item.class);
            itemCrit.add(Restrictions.sizeEq(Item.CATEGORIES_REF, 0));
            itemCrit.setProjection(Projections.distinct(Projections.rowCount()));
            unclassified.setItemCount((Integer) itemCrit.uniqueResult());
            categories.add(unclassified);
        }

        tx.commit();
        logger.debug("Rcupration russie!");
    } catch (Exception hex) {
        logger.error(hex);
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }

    return categories;
}

From source file:cc.cnfc.core.orm.hibernate.SimpleHibernateDao.java

License:Open Source License

/**
 * ?,??.//  w ww  .  j  a  va2 s  .  c  om
 */
public List<T> getAll(String orderBy, boolean isAsc) {
    Criteria c = createCriteria();
    if (isAsc) {
        c.addOrder(Order.asc(orderBy));
    } else {
        c.addOrder(Order.desc(orderBy));
    }
    return c.list();
}

From source file:ch.astina.hesperid.dao.hibernate.FilterGridDataSource.java

License:Apache License

@Override
protected void applyAdditionalConstraints(Criteria criteria) {
    for (String join : joins) {
        criteria.setFetchMode(join, FetchMode.JOIN);
    }/*from   ww w . jav a  2s.  c  om*/

    for (Entry<String, String> alias : aliases.entrySet()) {
        criteria.createAlias(alias.getKey(), alias.getValue());
    }

    for (Criterion filter : filters) {
        criteria.add(filter);
    }

    if (order != null) {
        criteria.addOrder(order);
    }
}

From source file:ch.qos.logback.audit.persistent.AuditEventDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
static public List<Object[]> findMaxObject() throws HibernateException {
    Session s = null;/*w ww  .  ja  v  a2  s  . c  om*/
    try {
        s = openSession();

        //Query q = s.createQuery("select count(ae.subject), ae.subject FROM "+ AE_CLASS + " AS ae "+
        //"GROUP BY ae.subject ORDER BY count(ae.subject) desc");

        Criteria crit = s.createCriteria(AuditEvent.class);
        crit.setProjection(Projections.projectionList().add(Projections.count("object").as("gcount"))
                .add(Projections.groupProperty("object")));
        crit.addOrder(Order.desc("gcount"));
        crit.setMaxResults(10);
        return crit.list();
    } finally {
        close(s);
    }
}

From source file:ch.tatool.app.service.impl.ModuleSessionDAO.java

License:Open Source License

/** Finds the last session for a given module. */
public ModuleSession findLastSession(final ModuleImpl module) {
    return (ModuleSession) getHibernateTemplate().execute(new HibernateCallback() {
        @SuppressWarnings("unchecked")
        public Object doInHibernate(Session session) {
            Criteria criteria = session.createCriteria(ModuleSession.class);
            criteria.add(Restrictions.eq("module", module));
            criteria.setFetchSize(1);/*w  ww .  ja  v a2 s.  c  o m*/
            criteria.addOrder(Order.desc("id"));
            List<ModuleSession> sessions = (List<ModuleSession>) criteria.list();
            if (sessions.size() > 0) {
                return sessions.get(0);
            } else {
                return null;
            }
        }
    });
}

From source file:chat.service.DoChat.java

License:LGPL

/**
 * read chats according to SO' {@link Chat#out} and {@link Chat#in} from
 * {@link Chat#datime}(excluded, or oldest if null), order by {@link Chat#datime} asc
 *//*from ww w. java  2  s. co m*/
@Service
@Transac.Readonly
public List<Chat> read(Chat c) throws Exception {
    Criteria<Chat> t = data.criteria(Chat.class);
    User me = new User().id(sess.me);

    Criterion out = Restrictions.eq("out", me);
    if (c.in != null)
        out = Restrictions.and(out, // chats from me
                Restrictions.eq("in", c.in));
    Criterion in = Restrictions.eq("in", me);
    if (c.out != null)
        in = Restrictions.and(in, // chats to me
                Restrictions.eq("out", c.out));
    t.add(Restrictions.or(out, in));
    if (c.datime != null)
        t.add(Restrictions.gt("datime", c.datime));
    return t.addOrder(Order.asc("datime")).list();
}

From source file:cn.com.jandar.lawyer.service.LawFirmService.java

@SuppressWarnings("unchecked")
public List<LawFirm> findByParam(Date lastUpdateDate, Integer pageNum, Integer pageSize) {
    Criteria c = lawFirmDao.getSession().createCriteria(LawFirm.class);
    if (lastUpdateDate != null) {
        c.add(Restrictions.ge("lastUpdateDate", lastUpdateDate));
    }//  w  w  w .j a  v  a  2s .  co m
    c.setMaxResults(pageSize);
    c.setFirstResult(pageNum * pageSize + 1);
    c.addOrder(Order.desc("laseUpdateDate"));
    return c.list();
}

From source file:cn.com.jandar.lawyer.service.LawyerService.java

/**
 * ?/*from www.j  a va 2 s . co  m*/
 * @param lastUpdateDate
 * @param pageNum
 * @param pageSize
 * @return
 */
@SuppressWarnings("unchecked")
public List<Lawyer> findByParam(Date lastUpdateDate, Integer pageNum, Integer pageSize) {
    Criteria c = lawyerDao.getSession().createCriteria(Lawyer.class);
    if (lastUpdateDate != null) {
        c.add(Restrictions.ge("lastUpdateDate", lastUpdateDate));
    }
    c.setMaxResults(pageSize);
    c.setFirstResult(pageNum * pageSize + 1);
    c.addOrder(Order.desc("laseUpdateDate"));
    return c.list();
}

From source file:cn.dayuanzi.dao.BaseDao.java

License:Apache License

/**
 *   ?, ??./*from  w  ww .j  av a2s.c o m*/
 */
public List<T> getAll(String orderByProperty, boolean isAsc, Criterion... criterions) {
    Criteria c = createCriteria(criterions);
    if (isAsc) {
        c.addOrder(Order.asc(orderByProperty));
    } else {
        c.addOrder(Order.desc(orderByProperty));
    }
    return c.list();
}

From source file:cn.dayuanzi.dao.BaseDao.java

License:Apache License

/**
 * ?/*from  www .j a  va 2  s.  c  om*/
 * 
 * @param current_page >=1
 * @param max_num >=1 <=1000
 * @param map
 * @param orderByProperty
 * @param isAsc
 * @return
 */
public List<T> findForPage(int current_page, int max_num, Map<String, Object> map, String orderByProperty,
        boolean isAsc) {
    List<T> result = null;
    if (current_page <= 0) {
        current_page = 1;
    }
    if (max_num > 20) {
        max_num = 20;
    } else if (max_num <= 0) {
        max_num = 1;
    }
    Criteria c = createCriteria();
    if (MapUtils.isNotEmpty(map)) {
        Iterator<String> it = map.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            c.add(Restrictions.eq(key, map.get(key)));
        }
    }
    c.setFirstResult(((current_page - 1)) * max_num);
    c.setMaxResults(max_num);
    if (StringUtils.isNotBlank(orderByProperty)) {
        if (isAsc) {
            c.addOrder(Order.asc(orderByProperty));
        } else {
            c.addOrder(Order.desc(orderByProperty));
        }
    }
    result = c.list();
    return result;
}