Example usage for org.hibernate.criterion Projections sum

List of usage examples for org.hibernate.criterion Projections sum

Introduction

In this page you can find the example usage for org.hibernate.criterion Projections sum.

Prototype

public static AggregateProjection sum(String propertyName) 

Source Link

Document

A property value sum projection

Usage

From source file:com.inkubator.hrm.dao.impl.TempAttendanceRealizationDaoImpl.java

@Override
public Long totalDaySchedule() {
    Criteria criteria = getCurrentSession().createCriteria(getEntityClass());
    Long result = (Long) criteria.setProjection(Projections.sum("attendanceDaysSchedule")).uniqueResult();
    return result == null ? 0l : result;
}

From source file:com.inkubator.hrm.dao.impl.TempAttendanceRealizationDaoImpl.java

@Override
public Long getTotalOverTime(long empId) {
    Criteria criteria = getCurrentSession().createCriteria(getEntityClass());
    criteria.createAlias("empData", "ce", JoinType.INNER_JOIN);
    criteria.add(Restrictions.eq("ce.id", empId));
    Long result = (Long) criteria.setProjection(Projections.sum("overtime")).uniqueResult();
    return result == null ? 0l : result;
}

From source file:com.klistret.cmdb.utility.hibernate.XPathCriteria.java

License:Open Source License

/**
 * Creates a Hibernate projection from an aggregate expression
 * //from   w ww .  j ava 2  s .  c o  m
 * @param expression
 */
public Projection aggregate(String expression) {
    logger.debug("Creating projection based on aggregate expression [{}]", expression);

    FunctionCall fc = new FunctionCall(expression);
    RelativePathExpr rpe = fc.getRelativePath();

    HibernateRelativePath hrp = translate(rpe);

    /**
     * Confirm the last Hibernate step is a Hibernate property
     */
    HibernateStep last = hrp.getLastHibernateStep();
    if (last.getType() != Type.Property)
        throw new ApplicationException("Aggregation must act either on a Hibernate property or an XML column");

    /**
     * Property name with alias
     */
    String alias = aliasCache.get(last.getPrevious().getPath());
    String propertyName = alias == null ? last.getName() : String.format("%s.%s", alias, last.getName());

    /**
     * Only sum, avg, max, and min supported
     */
    switch (fc.getFunction()) {
    case sum:
        return last.isXml() ? new XPathAggregation("sum", propertyName, last.getStep())
                : Projections.sum(propertyName);
    case avg:
        return last.isXml() ? new XPathAggregation("avg", propertyName, last.getStep())
                : Projections.avg(propertyName);
    case max:
        return last.isXml() ? new XPathAggregation("max", propertyName, last.getStep())
                : Projections.max(propertyName);
    case min:
        return last.isXml() ? new XPathAggregation("min", propertyName, last.getStep())
                : Projections.min(propertyName);
    default:
        throw new InfrastructureException(String.format("Function call [%s] not handled.", fc.getFunction()));
    }
}

From source file:com.kodemore.hibernate.criteria.KmCriteria.java

License:Open Source License

public void selectSum(String name) {
    Projection e;
    e = Projections.sum(getFullName(name));
    addProjection(e);
}

From source file:com.liferay.portal.dao.orm.hibernate.ProjectionFactoryImpl.java

License:Open Source License

public Projection sum(String propertyName) {
    return new ProjectionImpl(Projections.sum(propertyName));
}

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

public Long countVegetableFromWeekMenus(Vegetable vegetable) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(MenuItem.class);
    Criteria menuCriteria = criteria.createCriteria("menu");
    menuCriteria.add(Restrictions.eq("isWeekMenu", true));
    Criteria vegetableCriteria = criteria.createCriteria("vegetable");
    vegetableCriteria.add(Restrictions.eq("name", vegetable.getName()));
    criteria.setProjection(Projections.sum("quantity"));
    return (Long) criteria.uniqueResult();
    //        return (Long) criteria.list();
}

From source file:com.qcadoo.model.api.search.SearchProjections.java

License:Open Source License

/**
 * Creates projection which add given field to the "GROUP BY" clause and its "sum" to the "SELECT" clause.
 * /*from   w  ww .jav a 2 s  .  co m*/
 * @param field
 *            field
 * @return projection
 */
public static SearchProjection sum(final String field) {
    return new SearchProjectionImpl(Projections.sum(field));
}

From source file:com.qfix.vms.dao.impl.RepairDaoImpl.java

@Override
public int getsum(Date from, Date to) {
    Session session = sessionFactory.openSession();
    Transaction tx = null;/* w  w  w.  jav a  2 s . co  m*/

    tx = session.beginTransaction();
    Criteria cr = session.createCriteria(Repair.class);

    cr.add(Restrictions.between("date", from, to));
    cr.setProjection(Projections.sum("repairCost"));
    int repairs = cr.hashCode();
    return repairs;
}

From source file:com.reignite.query.StructuredQuery.java

License:Open Source License

public QueryResult execute() throws ParserException {
    QueryResult result = new QueryResult();

    result.setResult(new ArrayList<Object>());
    result.setEndIndex(0);/*from w ww  .  j  a  v a2 s.  co  m*/
    result.setStartIndex(0);
    if (!executed) {
        processGroups();
    }
    if (queryType == QueryType.COUNT) {
        if (!executed) {
            criteria.setProjection(Projections.rowCount());
        }
        Object obj = criteria.uniqueResult();
        if (obj != null) {
            result.getResult().add(obj);
        }
    } else if (queryType == QueryType.AVG) {
        if (!executed) {
            if (fields.size() == 0) {
                throw new ParserException("You must specify a single field to average.");
            }
            criteria.setProjection(Projections.avg(fields.get(0)));
        }
        Object obj = criteria.uniqueResult();
        if (obj != null) {
            result.getResult().add(obj);
        }
    } else if (queryType == QueryType.SUM) {
        if (!executed) {
            if (fields.size() == 0) {
                throw new ParserException("You must specify a single field to sum.");
            }
            criteria.setProjection(Projections.sum(fields.get(0)));
        }
        Object obj = criteria.uniqueResult();
        if (obj != null) {
            result.getResult().add(obj);
        }
    } else if (queryType == QueryType.LIST) {
        if (!executed) {
            processAggregates();
            processOrder();
            processFields();
        }
        int count = runQuery(criteria, result, maxResults);

        result.setStartIndex(startIndex);
        result.setEndIndex(startIndex + Math.min(count, maxResults));
        // merge joins
        if (hasJoin) {
            runJoin(result);
        }
    } else if (queryType == QueryType.LOAD) {
        if (!executed) {
            processFields();
            processOrder();
        }
        Object obj = criteria.uniqueResult();
        if (obj != null) {
            fillResult(result, obj);
        }
        result.setEndIndex(0);
        result.setStartIndex(0);
        result.setTotalResults(result.getResult().size());
        if (hasJoin) {
            runJoin(result);
        }
    }

    executed = true;
    return result;
}

From source file:com.reignite.query.StructuredQuery.java

License:Open Source License

public void addAggregate(QueryType type, String field) throws ParserException {
    switch (type) {
    case AVG://from  w  ww . ja  v  a  2  s.  com
        aggregates.put("avg(" + field + ")", Projections.avg(field));
        break;
    case COUNT:
        aggregates.put("count(" + field + ")", Projections.count(field));
        break;
    case SUM:
        aggregates.put("sum(" + field + ")", Projections.sum(field));
        break;
    default:
        throw new ParserException("Invalid aggregate type: " + type.name());
    }
}