Search in sources :

Example 91 with Criteria

use of org.hibernate.Criteria in project opennms by OpenNMS.

the class AbstractDaoHibernate method countMatching.

/**
 * {@inheritDoc}
 */
@Override
public int countMatching(final org.opennms.core.criteria.Criteria criteria) throws DataAccessException {
    final HibernateCallback<Integer> callback = new HibernateCallback<Integer>() {

        @Override
        public Integer doInHibernate(final Session session) throws HibernateException, SQLException {
            final Criteria hibernateCriteria = m_criteriaConverter.convertForCount(criteria, session);
            hibernateCriteria.setProjection(Projections.rowCount());
            Long retval = (Long) hibernateCriteria.uniqueResult();
            hibernateCriteria.setProjection(null);
            hibernateCriteria.setResultTransformer(Criteria.ROOT_ENTITY);
            return retval.intValue();
        }
    };
    Integer retval = getHibernateTemplate().execute(callback);
    return retval == null ? 0 : retval.intValue();
}
Also used : HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) Criteria(org.hibernate.Criteria) OnmsCriteria(org.opennms.netmgt.model.OnmsCriteria) Session(org.hibernate.Session)

Example 92 with Criteria

use of org.hibernate.Criteria in project opennms by OpenNMS.

the class AbstractDaoHibernate method findMatching.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
public List<T> findMatching(final OnmsCriteria onmsCrit) throws DataAccessException {
    // FIXME: why is this here?
    onmsCrit.resultsOfType(m_entityClass);
    final HibernateCallback<List<T>> callback = new HibernateCallback<List<T>>() {

        @Override
        public List<T> doInHibernate(final Session session) throws HibernateException, SQLException {
            final Criteria attachedCrit = onmsCrit.getDetachedCriteria().getExecutableCriteria(session);
            if (onmsCrit.getFirstResult() != null)
                attachedCrit.setFirstResult(onmsCrit.getFirstResult());
            if (onmsCrit.getMaxResults() != null)
                attachedCrit.setMaxResults(onmsCrit.getMaxResults());
            return (List<T>) attachedCrit.list();
        }
    };
    return getHibernateTemplate().execute(callback);
}
Also used : List(java.util.List) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) Criteria(org.hibernate.Criteria) OnmsCriteria(org.opennms.netmgt.model.OnmsCriteria) Session(org.hibernate.Session)

Example 93 with Criteria

use of org.hibernate.Criteria in project opennms by OpenNMS.

the class AbstractDaoHibernate method buildHibernateCallback.

protected <T> HibernateCallback<List<T>> buildHibernateCallback(org.opennms.core.criteria.Criteria criteria) {
    return new HibernateCallback<List<T>>() {

        @Override
        public List<T> doInHibernate(final Session session) throws HibernateException, SQLException {
            LOG.debug("criteria = {}", criteria);
            final Criteria hibernateCriteria = m_criteriaConverter.convert(criteria, session);
            return (List<T>) (hibernateCriteria.list());
        }
    };
}
Also used : List(java.util.List) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) Criteria(org.hibernate.Criteria) OnmsCriteria(org.opennms.netmgt.model.OnmsCriteria) Session(org.hibernate.Session)

Example 94 with Criteria

use of org.hibernate.Criteria in project opennms by OpenNMS.

the class AbstractDaoHibernate method countMatching.

/**
 * {@inheritDoc}
 */
public int countMatching(final OnmsCriteria onmsCrit) throws DataAccessException {
    final HibernateCallback<Integer> callback = new HibernateCallback<Integer>() {

        @Override
        public Integer doInHibernate(final Session session) throws HibernateException, SQLException {
            final Criteria attachedCrit = onmsCrit.getDetachedCriteria().getExecutableCriteria(session).setProjection(Projections.rowCount());
            Long retval = (Long) attachedCrit.uniqueResult();
            attachedCrit.setProjection(null);
            attachedCrit.setResultTransformer(Criteria.ROOT_ENTITY);
            return retval.intValue();
        }
    };
    Integer retval = getHibernateTemplate().execute(callback);
    return retval == null ? 0 : retval.intValue();
}
Also used : HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) Criteria(org.hibernate.Criteria) OnmsCriteria(org.opennms.netmgt.model.OnmsCriteria) Session(org.hibernate.Session)

Example 95 with Criteria

use of org.hibernate.Criteria in project java-design-patterns by iluwatar.

the class SpellDaoImpl method findByName.

@Override
public Spell findByName(String name) {
    Session session = getSessionFactory().openSession();
    Transaction tx = null;
    Spell result = null;
    try {
        tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(persistentClass);
        criteria.add(Restrictions.eq("name", name));
        result = (Spell) criteria.uniqueResult();
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        throw e;
    } finally {
        session.close();
    }
    return result;
}
Also used : Transaction(org.hibernate.Transaction) Criteria(org.hibernate.Criteria) Session(org.hibernate.Session)

Aggregations

Criteria (org.hibernate.Criteria)180 Session (org.hibernate.Session)95 Test (org.junit.Test)69 List (java.util.List)39 Transaction (org.hibernate.Transaction)39 DetachedCriteria (org.hibernate.criterion.DetachedCriteria)27 ArrayList (java.util.ArrayList)15 TestForIssue (org.hibernate.testing.TestForIssue)12 Iterator (java.util.Iterator)9 Period (org.hisp.dhis.period.Period)8 Map (java.util.Map)6 State (org.hibernate.test.cache.infinispan.functional.entities.State)5 OnmsCriteria (org.opennms.netmgt.model.OnmsCriteria)5 HibernateCallback (org.springframework.orm.hibernate3.HibernateCallback)5 HashSet (java.util.HashSet)3 Criterion (org.hibernate.criterion.Criterion)3 Statistics (org.hibernate.stat.Statistics)3 Pager (org.hisp.dhis.common.Pager)3 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)2 HashMap (java.util.HashMap)2