Example usage for org.hibernate Criteria setResultTransformer

List of usage examples for org.hibernate Criteria setResultTransformer

Introduction

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

Prototype

public Criteria setResultTransformer(ResultTransformer resultTransformer);

Source Link

Document

Set a strategy for handling the query results.

Usage

From source file:com.liferay.portal.workflow.jbpm.dao.CustomSession.java

License:Open Source License

public List<TaskInstance> findTaskInstances(long processInstanceId, long tokenId, String[] actorIds,
        boolean pooledActors, Boolean completed, int start, int end, OrderByComparator orderByComparator) {

    if ((actorIds != null) && (actorIds.length == 0)) {
        return Collections.emptyList();
    }//from  w ww.  j a v a  2 s  .  c om

    try {
        Criteria criteria = _session.createCriteria(TaskInstance.class);

        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

        if (processInstanceId > 0) {
            criteria.add(Restrictions.eq("processInstance.id", processInstanceId));
        } else if (tokenId > 0) {
            criteria.add(Restrictions.eq("token.id", tokenId));
        }

        if (actorIds != null) {
            if (pooledActors) {
                Criteria subcriteria = criteria.createCriteria("pooledActors");

                subcriteria.add(Restrictions.in("actorId", actorIds));

                criteria.add(Restrictions.isNull("actorId"));
            } else {
                criteria.add(Restrictions.in("actorId", actorIds));
            }
        }

        if (completed != null) {
            if (completed.booleanValue()) {
                criteria.add(Restrictions.isNotNull("end"));
            } else {
                criteria.add(Restrictions.isNull("end"));
            }
        }

        addPagination(criteria, start, end);
        addOrder(criteria, orderByComparator);

        return criteria.list();
    } catch (Exception e) {
        throw new JbpmException(e);
    }
}

From source file:com.liferay.portal.workflow.jbpm.dao.CustomSession.java

License:Open Source License

protected List<TaskInstance> getTasksInstancesBySubmittingUser(long companyId, long userId, Boolean completed,
        int start, int end, OrderByComparator orderByComparator) {

    List<TaskInstance> taskInstances = new ArrayList<TaskInstance>();

    try {/*from w w  w .jav a  2s  .  c  o m*/
        Criteria criteria = _session.createCriteria(TaskInstance.class);

        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

        if (completed != null) {
            if (completed.booleanValue()) {
                criteria.add(Restrictions.isNotNull("end"));
            } else {
                criteria.add(Restrictions.isNull("end"));
            }
        }

        addOrder(criteria, orderByComparator);

        for (TaskInstance taskInstance : (List<TaskInstance>) criteria.list()) {

            ProcessInstance processInstance = taskInstance.getProcessInstance();

            ContextInstance contextInstance = processInstance.getContextInstance();

            long taskInstanceUserId = GetterUtil.getLong((String) contextInstance.getVariable("userId"));

            if (userId == taskInstanceUserId) {
                taskInstances.add(taskInstance);
            }
        }
    } catch (Exception e) {
        throw new JbpmException(e);
    }

    if ((end != QueryUtil.ALL_POS) && (taskInstances.size() > end)) {
        taskInstances = ListUtil.subList(taskInstances, start, end);
    }

    return taskInstances;
}

From source file:com.lighting.platform.base.dao.HibernateDao.java

License:Apache License

/**
 * countCriteria.//from   w  w  w  . j ava  2 s  .c o m
 */
protected long countCriteriaResult(final Criteria c) {
    org.hibernate.internal.CriteriaImpl impl = (CriteriaImpl) c;

    // Projection?ResultTransformer?OrderBy??,??Count?
    Projection projection = impl.getProjection();
    ResultTransformer transformer = impl.getResultTransformer();

    List<CriteriaImpl.OrderEntry> orderEntries = null;
    try {
        orderEntries = (List) ReflectionUtils.getFieldValue(impl, "orderEntries");
        ReflectionUtils.setFieldValue(impl, "orderEntries", new ArrayList());
    } catch (Exception e) {
        logger.error("??:" + e.getMessage());
    }

    // Count
    Long totalCountObject = (Long) c.setProjection(Projections.rowCount()).uniqueResult();
    long totalCount = (totalCountObject != null) ? totalCountObject : 0;

    // ?Projection,ResultTransformerOrderBy??
    c.setProjection(projection);

    if (projection == null) {
        c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
    }
    if (transformer != null) {
        c.setResultTransformer(transformer);
    }
    try {
        ReflectionUtils.setFieldValue(impl, "orderEntries", orderEntries);
    } catch (Exception e) {
        logger.error("??:" + e.getMessage());
    }

    return totalCount;
}

From source file:com.lighting.platform.base.dao.SimpleHibernateDao.java

License:Apache License

/**
 * Criteriadistinct transformer.//from w w  w  .ja  v a  2s.  c  o  m
 * ?HQL??, ?distinct?.
 */
public Criteria distinct(Criteria criteria) {
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    return criteria.setCacheable(cacheable());
}

From source file:com.mac.green_leaves.v1.dashboard.receive_dashboard.DashboardService.java

List<TGreenLeavesWeigh> getGeenLeavesWeighTotalSummary(greenLeavesSummry leavesSummry) {
    Criteria criteria = entityManager.unwrap(Session.class).createCriteria(TGreenLeavesWeigh.class);
    criteria.add(Restrictions.eq("type", leavesSummry.getType()));

    if (leavesSummry.getBranch() != null) {
        criteria.add(Restrictions.eq("branch", leavesSummry.getBranch()));
    }//from www.j av  a2  s.  c  o  m

    if (leavesSummry.getFromDate() != null && leavesSummry.getToDate() != null) {
        criteria.add(Restrictions.between("date", leavesSummry.getFromDate(), leavesSummry.getToDate()));
    }

    if (leavesSummry.getRoute() != null) {
        criteria.add(Restrictions.eq("route", leavesSummry.getRoute()));
    }

    if (!"null".equals(leavesSummry.getStatus())) {
        if (leavesSummry.getStatus() != null) {
            criteria.add(Restrictions.eq("status", leavesSummry.getStatus()));
        }
    }

    if (leavesSummry.getClient() != null) {
        criteria.add(Restrictions.eq("client", leavesSummry.getClient()));
    }

    if (leavesSummry.getRouteOfficer() != null) {
        criteria.add(Restrictions.eq("routeOfficer", leavesSummry.getRouteOfficer()));
    }

    if (leavesSummry.getRouteHelper() != null) {
        criteria.add(Restrictions.eq("routeHelper", leavesSummry.getRouteHelper()));
    }

    if (leavesSummry.getVehicle() != null) {
        criteria.add(Restrictions.eq("vehicle", leavesSummry.getVehicle()));
    }

    if (leavesSummry.getFromDate() == null && leavesSummry.getToDate() == null
            && leavesSummry.getRoute() == null && leavesSummry.getClient() == null
            && leavesSummry.getRouteOfficer() == null && leavesSummry.getRouteHelper() == null
            && leavesSummry.getVehicle() == null) {
        throw new EntityNotFoundException("green leaves weigh not found");
    }

    criteria.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);
    criteria.addOrder(Order.asc("date"));
    criteria.add(Restrictions.ne("status", "DELETED"));
    List<TGreenLeavesWeigh> greenLeavesWeigh = criteria.list();
    return greenLeavesWeigh;
}

From source file:com.mac.green_leaves.v1.dashboard.receive_dashboard.DashboardService.java

List<TGreenLeavesReceive> getGeenLeavesReceiveTotalSummary(greenLeavesSummry leavesSummry) {
    if ("client".equals(leavesSummry.getType())) {
        Criteria criteria = entityManager.unwrap(Session.class).createCriteria(TGreenLeavesReceive.class);

        if (leavesSummry.getBranch() != null) {
            criteria.add(Restrictions.eq("branch", leavesSummry.getBranch()));
        }//w  ww  . ja  va2s  .c o  m

        if (leavesSummry.getFromDate() != null && leavesSummry.getToDate() != null) {
            criteria.add(Restrictions.between("date", leavesSummry.getFromDate(), leavesSummry.getToDate()));
        }

        if (!"null".equals(leavesSummry.getStatus())) {
            if (leavesSummry.getStatus() != null) {
                criteria.add(Restrictions.eq("status", leavesSummry.getStatus()));
            }
        }

        if (leavesSummry.getRoute() != null) {
            criteria.add(Restrictions.eq("route", leavesSummry.getRoute()));
        }

        if (leavesSummry.getClient() != null) {
            System.out.println(leavesSummry.getClient());
            criteria.createAlias("greenLeavesReceiveDetails", "glrd");
            criteria.add(Restrictions.eq("glrd.client", leavesSummry.getClient()));
        }

        if (leavesSummry.getClient() == null && leavesSummry.getRoute() == null
                && leavesSummry.getFromDate() == null && leavesSummry.getToDate() == null) {
            throw new EntityNotFoundException("green leaves receive not found");
        }

        criteria.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);
        criteria.addOrder(Order.asc("date"));
        //get only bulk receive
        criteria.add(Restrictions.eq("type", "BULK"));
        criteria.add(Restrictions.ne("status", "DELETED"));
        List<TGreenLeavesReceive> greenLeavesReceives = criteria.list();
        return greenLeavesReceives;

    } else {
        Criteria criteria = entityManager.unwrap(Session.class).createCriteria(TGreenLeavesReceive.class);

        if (leavesSummry.getBranch() != null) {
            criteria.add(Restrictions.eq("branch", leavesSummry.getBranch()));
        }

        if (leavesSummry.getFromDate() != null && leavesSummry.getToDate() != null) {
            criteria.add(Restrictions.between("date", leavesSummry.getFromDate(), leavesSummry.getToDate()));
        }

        if (!"null".equals(leavesSummry.getStatus())) {
            if (leavesSummry.getStatus() != null) {
                criteria.add(Restrictions.eq("status", leavesSummry.getStatus()));
            }
        }

        if (leavesSummry.getRoute() != null) {
            criteria.add(Restrictions.eq("route", leavesSummry.getRoute()));
        }

        if (leavesSummry.getClient() == null && leavesSummry.getRoute() == null
                && leavesSummry.getFromDate() == null && leavesSummry.getToDate() == null) {
            throw new EntityNotFoundException("green leaves receive not found");
        }

        criteria.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);
        criteria.addOrder(Order.asc("date"));
        //get only bulk receive
        criteria.add(Restrictions.eq("type", "BULK"));
        criteria.add(Restrictions.ne("status", "DELETED"));
        List<TGreenLeavesReceive> greenLeavesReceives = criteria.list();
        return greenLeavesReceives;
    }
}

From source file:com.micromap.dao.AuthoritiesDAO.java

public List<Authorities> listaAuthorities() throws Exception {
    List<Authorities> lista = null;
    try {//from   w w w  .j av  a2s  .c  o m
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Authorities.class);
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        lista = criteria.list();
    } catch (Exception e) {
        throw new Exception(e.getMessage());
    } finally {
        session.close();
    }
    return lista;
}

From source file:com.micromap.dao.AuthoritiesDAO.java

public Authorities buscaAuthorities(String authority) throws Exception {
    Authorities retorno = null;/*  w  w  w  . ja v  a  2 s.c o m*/
    try {
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Authorities.class);
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        criteria.add(Restrictions.like("authority", authority));
        retorno = (Authorities) criteria.uniqueResult();
    } catch (Exception e) {
        throw new Exception(e.getMessage());
    } finally {
        session.close();
    }
    return retorno;
}

From source file:com.micromap.dao.UsersDAO.java

public List<Users> listaUsers() throws Exception {
    List<Users> lista = null;
    try {/*from   ww  w .  j a  v a2  s . c o m*/
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Users.class);
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        lista = criteria.list();
    } catch (Exception e) {
        throw new Exception(e.getMessage());
    } finally {
        session.close();
    }
    return lista;
}

From source file:com.mil.randommenu.dao.MenuDao.java

public List<Menu> getByIsWeekMenu(Boolean isWeekMenu) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Menu.class);
    criteria.add(Restrictions.eq("isWeekMenu", isWeekMenu));
    criteria.addOrder(Order.asc("name"));
    return criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
}