/** 첫번째 낙서를 찾아서 삭제한다. 첫번째 낙서는 가장 낮은 id 를 가진 Graffiti 를 기준으로 한다. */ public void deleteFirstGraffiti() throws DataAccessException { deleteById( (Integer) getCurrentSession() .createCriteria(Graffiti.class) .setProjection(Projections.min("id")) .uniqueResult()); }
@Override public Object findMin(String min, Map<String, Object> Map) { Criteria criteria = createCriteria(); for (Iterator<String> i = Map.keySet().iterator(); i.hasNext(); ) { String name = i.next(); if (filterProperty(name)) { criteria.add(Restrictions.eq(name, Map.get(name))); } else { log.error("Could not resolve property:" + name); } } return criteria.setProjection(Projections.min(min)).uniqueResult(); }
@Override public Projection buildProjection( String projectionField, ProjectionType projectionType, Iterator argsIterator) { Projection projection = new Projection(); projection.setField(projectionField); switch (projectionType) { case RowCount: projection.setDetails(Projections.rowCount()); break; case Count: projection.setDetails(Projections.count(projectionField)); break; case CountDistinct: projection.setDetails(Projections.countDistinct(projectionField)); break; case Maximum: projection.setDetails(Projections.max(projectionField)); break; case Minimum: projection.setDetails(Projections.min(projectionField)); break; case Average: projection.setDetails(Projections.avg(projectionField)); break; case Sum: projection.setDetails(Projections.sum(projectionField)); break; default: throw new RuntimeException("Unexpected projection type: " + projectionType); } return projection; }
public static void main(String... args) { final String[] columns = {"max", "min", "sum", "avg"}; try { Session session = HibernateUtil.getSession(); Transaction tx = session.beginTransaction(); Criteria criteria = session.createCriteria(Product.class); ProjectionList projList = Projections.projectionList(); projList.add(Projections.max("price")); projList.add(Projections.min("price")); projList.add(Projections.sum("price")); projList.add(Projections.avg("price")); criteria.setProjection(projList); Object[] results = (Object[]) criteria.uniqueResult(); tx.commit(); for (int i = 0, n = results.length; i < n; i++) { Object res = results[i]; System.out.printf("%s = %s%n", columns[i], res); } } finally { HibernateUtil.cleanUp(); } }