Example usage for org.hibernate.criterion Projections max

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

Introduction

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

Prototype

public static AggregateProjection max(String propertyName) 

Source Link

Document

A property maximum value projection

Usage

From source file:net.longfalcon.newsj.persistence.hibernate.BinaryDAOImpl.java

License:Open Source License

@Override
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public Timestamp findMaxDateAddedBinaryByReleaseNameProcStatGroupIdFromName(String relName, int procStat,
        long groupId, String fromName) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Binary.class);
    criteria.add(Restrictions.eq("relName", relName));
    criteria.add(Restrictions.eq("procStat", procStat));
    criteria.add(Restrictions.eq("groupId", groupId));
    criteria.add(Restrictions.eq("fromName", fromName));
    criteria.setProjection(Projections.max("dateAdded"));

    return (Timestamp) criteria.uniqueResult();
}

From source file:net.longfalcon.newsj.persistence.hibernate.ReleaseDAOImpl.java

License:Open Source License

@Override
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public Date getLastReleaseDateByRegexId(long regexId) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Release.class);
    criteria.add(Restrictions.eq("regexId", regexId));
    criteria.setProjection(Projections.max("addDate"));

    return (Date) criteria.uniqueResult();
}

From source file:net.purnama.pureff.dao.RateDao.java

public RateEntity getLastRate(CurrencyEntity currency) {
    DetachedCriteria maxQuery = DetachedCriteria.forClass(RateEntity.class);
    maxQuery.add(Restrictions.eq("currency", currency));
    maxQuery.setProjection(Projections.max("lastmodified"));

    Session session = this.sessionFactory.getCurrentSession();
    Criteria c = session.createCriteria(RateEntity.class);
    c.add(Restrictions.eq("currency", currency));
    c.add(Property.forName("lastmodified").eq(maxQuery));

    RateEntity rate = (RateEntity) c.uniqueResult();

    return rate;//w  w  w .ja  v  a  2s.  co  m
}

From source file:nl.edia.sakai.tool.skinmanager.impl.SkinArchiveServiceImpl.java

License:Educational Community License

@Override
public SkinArchive createSkinArchive(final String name, final InputStream file, final Date time,
        final String comment) {
    return (SkinArchive) getHibernateTemplate().execute(new HibernateCallback() {
        @Override/*  w  w  w  .  ja v a  2s .  c o  m*/
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            int myVersion = 0;
            Number myHighestVersion = (Number) session.createCriteria(SkinArchive.class)
                    .add(Restrictions.eq("name", name)).setProjection(Projections.max("version"))
                    .uniqueResult();
            if (myHighestVersion != null) {
                myVersion = myHighestVersion.intValue() + 1;
            }

            SkinArchive mySkinArchive = new SkinArchive();
            mySkinArchive.setActive(true);
            mySkinArchive.setLastModified(new Timestamp(time.getTime()));
            mySkinArchive.setName(name);
            mySkinArchive.setVersion(myVersion);
            mySkinArchive.setComment(comment);
            try {
                mySkinArchive.setFile(Hibernate.createBlob(file));
            } catch (IOException e) {
                throw new HibernateException(e);
            }
            session.save(mySkinArchive);
            setSkinStatus(name, true);
            return mySkinArchive;
        }
    });
}

From source file:org.ambraproject.migration.BootstrapMigratorServiceImpl.java

License:Apache License

private boolean isMigrateRunning() {
    return (Boolean) hibernateTemplate.execute(new HibernateCallback() {
        @Override// ww  w . j  av  a  2s .com
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            SQLQuery q = session.createSQLQuery("show tables");
            List<String> tables = q.list();

            //Check to see if the version table exists.
            //If it does not exist then no migrations have been run yet
            if (!tables.contains("version")) {
                return false;
            }

            //If we get this far, return the version column out of the database
            Criteria c = session.createCriteria(Version.class).setProjection(Projections.max("version"));

            int version = (Integer) c.uniqueResult();

            c = session.createCriteria(Version.class).add(Restrictions.eq("version", version));

            Version v = (Version) c.uniqueResult();

            return (v == null) ? false : v.getUpdateInProcess();
        }
    });
}

From source file:org.ambraproject.migration.BootstrapMigratorServiceImpl.java

License:Apache License

@SuppressWarnings("unchecked")
private void setDatabaseVersion() {
    this.dbVersion = ((Integer) hibernateTemplate.execute(new HibernateCallback() {
        @Override/*from  w ww. ja  v  a2 s.  co m*/
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            SQLQuery q = session.createSQLQuery("show tables");
            List<String> tables = q.list();

            //Check to see if the version table exists.
            //If it does not exist then it's ambra 2.00
            if (!tables.contains("version")) {
                return 210;
            }

            //If we get this far, return the version column out of the database
            Criteria c = session.createCriteria(Version.class).setProjection(Projections.max("version"));

            Integer i = (Integer) c.uniqueResult();

            return (i == null) ? 210 : c.uniqueResult();
        }
    }));
}

From source file:org.ambraproject.service.migration.BootstrapMigratorServiceImpl.java

License:Apache License

private boolean isMigrateRunning() {
    return (Boolean) hibernateTemplate.execute(new HibernateCallback() {
        @Override/*w  w w .  ja  v a  2  s .  co  m*/
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            SQLQuery q = session.createSQLQuery("show tables");
            List<String> tables = q.list();

            //Check to see if the version table exists.
            //If it does not exist then no migrations have been run yet
            if (!tables.contains("version")) {
                return false;
            }

            //If we get this far, return the version column out of the database
            Criteria c = session.createCriteria(Version.class).setProjection(Projections.max("version"));

            Integer version = (Integer) c.uniqueResult();
            if (version == null) {
                return false; // no migrations have been run yet
            }

            c = session.createCriteria(Version.class).add(Restrictions.eq("version", version));

            Version v = (Version) c.uniqueResult();

            return (v == null) ? false : v.getUpdateInProcess();
        }
    });
}

From source file:org.ambraproject.service.migration.BootstrapMigratorServiceImpl.java

License:Apache License

@SuppressWarnings("unchecked")
private int fetchDatabaseVersion() {
    return hibernateTemplate.execute(new HibernateCallback<Integer>() {
        @Override//  w  w w  .j a  v  a2 s .c  o m
        public Integer doInHibernate(Session session) throws HibernateException, SQLException {
            SQLQuery q = session.createSQLQuery("show tables");
            List<String> tables = q.list();

            //Check to see if the version table exists.
            //If it does not exist then it's ambra 2.00
            if (!tables.contains("version")) {
                return LegacyMigration.MIN_VERSION;
            }

            //If we get this far, return the version column out of the database
            Criteria c = session.createCriteria(Version.class).setProjection(Projections.max("version"));

            Integer i = (Integer) c.uniqueResult();

            return (i == null) ? LegacyMigration.MIN_VERSION : i;
        }
    });
}

From source file:org.apache.ode.daohib.bpel.ProcessInstanceDaoImpl.java

License:Apache License

public EventsFirstLastCountTuple getEventsFirstLastCount() {
    entering("ProcessInstanceDaoImpl.getEventsFirstLastCount");
    // Using a criteria, find the min,max, and count of event tstamps.
    Criteria c = getSession().createCriteria(HBpelEvent.class);
    c.add(Restrictions.eq("instance", _instance));
    c.setProjection(Projections.projectionList().add(Projections.min("tstamp")).add(Projections.max("tstamp"))
            .add(Projections.count("tstamp")));

    Object[] ret = (Object[]) c.uniqueResult();
    EventsFirstLastCountTuple flc = new EventsFirstLastCountTuple();
    flc.first = (Date) ret[0];//from  www  .j a  va  2 s .  c  o  m
    flc.last = (Date) ret[1];
    flc.count = (Integer) ret[2];
    return flc;
}

From source file:org.apache.usergrid.apm.service.charts.service.NetworkMetricsChartUtil.java

License:Apache License

public static ProjectionList getProjectionList(MetricsChartCriteria cq) {
    ProjectionList projList = Projections.projectionList();
    //Adding GroupBy. We will allow only one groupby so that chart looks cleaner.
    if (cq.isGroupedByApp()) {
        projList.add(Projections.groupProperty("this.appId"), "appId");
    } else//from www  .ja  v  a 2  s .  co m
        projList.add(Projections.property("this.appId"), "appId");

    //projList.add(Projections.groupProperty("appId"),"appId");

    if (cq.isGroupedByNetworkType()) {
        projList.add(Projections.groupProperty("this.networkType"), "networkType");
    }

    else if (cq.isGroupedByNetworkCarrier()) {
        projList.add(Projections.groupProperty("this.networkCarrier"), "networkCarrier");
    }

    else if (cq.isGroupedByAppVersion()) {
        projList.add(Projections.groupProperty("this.applicationVersion"), "applicationVersion");
    } else if (cq.isGroupedByAppConfigType()) {
        projList.add(Projections.groupProperty("this.appConfigType"), "appConfigType");
    } else if (cq.isGroupedByDeviceModel()) {
        projList.add(Projections.groupProperty("this.deviceModel"), "deviceModel");
    } else if (cq.isGroupedbyDevicePlatform()) {
        projList.add(Projections.groupProperty("this.devicePlatform"), "devicePlatform");
    }

    switch (cq.getSamplePeriod()) {
    //see http://stackoverflow.com/questions/84644/hibernate-query-by-example-and-projections on why "this." is needed 
    //in following lines
    case MINUTE:
        projList.add(Projections.groupProperty("this.endMinute"), "endMinute");
        break;
    case HOUR:
        projList.add(Projections.groupProperty("this.endHour"), "endHour");
        break;
    case DAY_WEEK:
        projList.add(Projections.groupProperty("this.endDay"), "endDay");
        break;
    case DAY_MONTH:
        projList.add(Projections.groupProperty("this.endDay"), "endDay");
        break;
    case MONTH:
        projList.add(Projections.groupProperty("this.endMonth"), "endMonth");
        break;
    }

    //Adding Projections

    projList.add(Projections.sum("numSamples"), "numSamples");
    projList.add(Projections.sum("numErrors"), "numErrors");
    projList.add(Projections.sum("sumLatency"), "sumLatency");
    projList.add(Projections.max("maxLatency"), "maxLatency");
    projList.add(Projections.min("minLatency"), "minLatency");

    //may run into this bug because of alias http://stackoverflow.com/questions/84644/hibernate-query-by-example-and-projections
    //And I did run into it. ouch. Fix was to add this.filedName !!

    return projList;

}