List of usage examples for org.hibernate.criterion Projections avg
public static AggregateProjection avg(String propertyName)
From source file:ar.com.zauber.commons.repository.aggregate.ProjectionAggregateFunctionVisitor.java
License:Apache License
/** crea projecciones en base a {@link PropertyAggregateFunction}. */ private static Projection createPropertyProjection(final PropertyAggregateFunction paf) { final String propertyName = paf.getProperty(); final Projection projection; if (paf instanceof AveragePropertyAggregateFunction) { projection = Projections.avg(propertyName); } else if (paf instanceof CountDistinctPropertyAggregateFunction) { projection = Projections.countDistinct(propertyName); } else if (paf instanceof CountPropertyAggregateFunction) { projection = Projections.count(propertyName); } else if (paf instanceof MaxPropertyAggregateFunction) { projection = Projections.max(propertyName); } else if (paf instanceof MinPropertyAggregateFunction) { projection = Projections.min(propertyName); } else if (paf instanceof SumPropertyAggregateFunction) { projection = Projections.sum(propertyName); } else if (paf instanceof GroupPropertyAggregateFilter) { projection = Projections.groupProperty(propertyName); } else {//from w w w . j a va 2 s .co m throw new IllegalArgumentException("don't know how to process " + paf.getClass()); } return projection; }
From source file:com.heliosapm.aa4h.parser.XMLQueryParser.java
License:Apache License
/** * Adds a new projection to the current projectionList stack. * Mandatory Attributes:<ul>// w w w . j a v a 2 s .c o m * <li><b>name</b>: The field to create a projection on.</li> * <li><b>type</b>: The projection type.</li> * <li><b>alias</b>: The name applied to the projection returned field.</li> * </ul> * @param attrs The attributes of the processed node. * @throws SAXException * TODO: Implement checks for mandatory attributes. */ public void addProjection(Attributes attrs) throws SAXException { ProjectionList projectionList = projectionStack.peek(); if (projectionList == null) { throw new SAXException( "Attempted to add Projection and no ProjectionList Exists On The Stack. (Projection must be embedded inside a Projections)"); } String type = attrs.getValue("type"); String name = attrs.getValue("name"); String alias = attrs.getValue("alias"); if ("avg".equalsIgnoreCase(type)) { projectionList.add(Projections.avg(name), alias); } else if ("count".equalsIgnoreCase(type)) { projectionList.add(Projections.count(name), alias); } else if ("countDistinct".equalsIgnoreCase(type)) { projectionList.add(Projections.countDistinct(name), alias); } else if ("groupProperty".equalsIgnoreCase(type)) { projectionList.add(Projections.groupProperty(name), alias); } else if ("max".equalsIgnoreCase(type)) { projectionList.add(Projections.max(name), alias); } else if ("min".equalsIgnoreCase(type)) { projectionList.add(Projections.min(name), alias); } else if ("sum".equalsIgnoreCase(type)) { projectionList.add(Projections.sum(name), alias); } else if ("rowCount".equalsIgnoreCase(type)) { projectionList.add(Projections.rowCount(), alias); } }
From source file:com.hibernate.dao.AsesinosDAO.java
@Override public List<Asesinos> getAsesinosListByProjection() { List<Asesinos> peliculas1 = session.createCriteria(Asesinos.class)//.list(); .setProjection(Projections.projectionList().add(Projections.rowCount()) .add(Projections.avg("personasasesinadas")).add(Projections.max("personasasesinadas")) .add(Projections.groupProperty("formato").as("ao")) ).list();/*from w w w . j av a2 s . c o m*/ return peliculas1; }
From source file:com.klistret.cmdb.utility.hibernate.XPathCriteria.java
License:Open Source License
/** * Creates a Hibernate projection from an aggregate expression * //from www . ja v a2 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 selectAverage(String name) { Projection e; e = Projections.avg(getFullName(name)); addProjection(e); }
From source file:com.liferay.portal.dao.orm.hibernate.ProjectionFactoryImpl.java
License:Open Source License
public Projection avg(String propertyName) { return new ProjectionImpl(Projections.avg(propertyName)); }
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 "avg" to the "SELECT" clause. * /*from w w w . jav a2s . c om*/ * @param field * field * @return projection */ public static SearchProjection avg(final String field) { return new SearchProjectionImpl(Projections.avg(field)); }
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);// www .ja v a 2s .c om 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 w w. ja v a 2 s .c o m*/ 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()); } }
From source file:com.salesmanager.core.service.catalog.impl.db.dao.ReviewDao.java
License:Open Source License
public Counter countAverageRatingByProduct(long productId) { try {//from ww w. j a va 2 s. c om Criteria c = super.getSession().createCriteria(Review.class) .add(Restrictions.eq("productId", productId)); c.setProjection( Projections.projectionList().add(Projections.avg("reviewRating")).add(Projections.rowCount())); List resp = c.list(); Counter counter = new Counter(); if (resp != null && resp.size() > 0) { Iterator i = resp.iterator(); while (i.hasNext()) { Object[] o = (Object[]) i.next(); counter.setAverage(((Double) o[0])); counter.setCount((Integer) o[1]); } } return counter; } catch (RuntimeException re) { log.error("get failed", re); throw re; } }