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();
}
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);
}
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());
}
};
}
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();
}
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;
}
Aggregations