Search in sources :

Example 51 with Criteria

use of org.hibernate.Criteria in project hibernate-orm by hibernate.

the class ReadWriteTest method getState.

private State getState(Session s, String name) {
    Criteria criteria = s.createCriteria(State.class);
    criteria.add(Restrictions.eq("name", name));
    criteria.setCacheable(true);
    return (State) criteria.list().get(0);
}
Also used : State(org.hibernate.test.cache.infinispan.functional.entities.State) Criteria(org.hibernate.Criteria)

Example 52 with Criteria

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

the class AbstractDaoHibernate method findMatching.

@SuppressWarnings("unchecked")
@Override
public List<T> findMatching(final org.opennms.core.criteria.Criteria criteria) {
    final HibernateCallback<List<T>> callback = 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());
        }
    };
    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 53 with Criteria

use of org.hibernate.Criteria in project ACS by ACS-Community.

the class TestPojosPersistence method testCriteriaAPI.

public void testCriteriaAPI() throws Exception {
    createDB();
    try {
        createConfigurationComputerAndTwoNetworkDevices();
        Configuration config = (Configuration) hibernateUtil.getList(Configuration.class).iterator().next();
        assertNotNull(config);
        // Now we test that using the criteria API we can find our objects
        Criteria c = hibernateUtil.getSession().createCriteria(NetworkDevice.class);
        c.add(Restrictions.eq("name", "wall-e"));
        assertEquals(2, c.list().size());
        c = hibernateUtil.getSession().createCriteria(NetworkDevice.class);
        c.add(Restrictions.eq("name", "wall-e"));
        c.add(Restrictions.eq("networkName", "wall-e.eso.org"));
        assertEquals(1, c.list().size());
        c = hibernateUtil.getSession().createCriteria(NetworkDevice.class);
        c.add(Restrictions.eq("configuration", config));
        assertEquals(3, c.list().size());
        c = hibernateUtil.getSession().createCriteria(Configuration.class);
        c.add(Restrictions.eq("configurationName", "rtobarConfig"));
        c.add(Restrictions.lt("creationTime", new Date()));
        assertEquals(1, c.list().size());
        try {
            c = hibernateUtil.getSession().createCriteria(Configuration.class);
            // typo: should be configurationName
            c.add(Restrictions.eq("configuratioName", "rtobarConfig"));
            c.list();
            fail("Should fail, property 'configuratioName' doesn't exist for Configuration objects");
        } catch (QueryException e) {
        }
    } finally {
        dropDB();
    }
}
Also used : QueryException(org.hibernate.QueryException) Criteria(org.hibernate.Criteria) Date(java.util.Date)

Example 54 with Criteria

use of org.hibernate.Criteria in project hibernate-orm by hibernate.

the class SpatialProjections method extent.

/**
	 * Applies an extent projection to the specified geometry function
	 *
	 * <p>The extent of a set of {@code Geometry}s is the union of their bounding boxes.</p>
	 *
	 * @param propertyName The property to use for calculating the extent
	 *
	 * @return an extent-projection for the specified property.
	 */
public static Projection extent(final String propertyName) {
    return new SimpleProjection() {

        public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
            return new Type[] { criteriaQuery.getType(criteria, propertyName) };
        }

        public String toSqlString(Criteria criteria, int position, CriteriaQuery criteriaQuery) throws HibernateException {
            final StringBuilder stbuf = new StringBuilder();
            final SessionFactoryImplementor factory = criteriaQuery.getFactory();
            final String[] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName);
            final Dialect dialect = factory.getDialect();
            if (dialect instanceof SpatialDialect) {
                final SpatialDialect seDialect = (SpatialDialect) dialect;
                stbuf.append(seDialect.getSpatialAggregateSQL(columns[0], SpatialAggregate.EXTENT));
                stbuf.append(" as y").append(position).append('_');
                return stbuf.toString();
            }
            return null;
        }
    };
}
Also used : Type(org.hibernate.type.Type) SpatialDialect(org.hibernate.spatial.SpatialDialect) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) CriteriaQuery(org.hibernate.criterion.CriteriaQuery) Dialect(org.hibernate.dialect.Dialect) SpatialDialect(org.hibernate.spatial.SpatialDialect) Criteria(org.hibernate.Criteria) SimpleProjection(org.hibernate.criterion.SimpleProjection)

Example 55 with Criteria

use of org.hibernate.Criteria in project hibernate-orm by hibernate.

the class TestSpatialRestrictions method retrieveAndCompare.

private void retrieveAndCompare(Map<Integer, Boolean> dbexpected, Criterion spatialCriterion) {
    Session session = null;
    Transaction tx = null;
    try {
        session = openSession();
        tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(GeomEntity.class);
        criteria.add(spatialCriterion);
        compare(dbexpected, criteria.list());
    } finally {
        if (tx != null) {
            tx.rollback();
        }
        if (session != null) {
            session.close();
        }
    }
}
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