Search in sources :

Example 1 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 2 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)

Example 3 with Criteria

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

the class ReadWriteTest method testNaturalIdCached.

@Test
public void testNaturalIdCached() throws Exception {
    saveSomeCitizens();
    // Clear the cache before the transaction begins
    cleanupCache();
    TIME_SERVICE.advance(1);
    withTxSession(s -> {
        State france = ReadWriteTest.this.getState(s, "Ile de France");
        Criteria criteria = s.createCriteria(Citizen.class);
        criteria.add(Restrictions.naturalId().set("ssn", "1234").set("state", france));
        criteria.setCacheable(true);
        Statistics stats = sessionFactory().getStatistics();
        stats.setStatisticsEnabled(true);
        stats.clear();
        assertEquals("Cache hits should be empty", 0, stats.getNaturalIdCacheHitCount());
        List results = criteria.list();
        assertEquals(1, results.size());
        assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
        assertEquals("NaturalId Cache Misses", 1, stats.getNaturalIdCacheMissCount());
        assertEquals("NaturalId Cache Puts", 1, stats.getNaturalIdCachePutCount());
        assertEquals("NaturalId Cache Queries", 1, stats.getNaturalIdQueryExecutionCount());
        criteria.list();
        assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
        assertEquals("NaturalId Cache Misses", 1, stats.getNaturalIdCacheMissCount());
        assertEquals("NaturalId Cache Puts", 1, stats.getNaturalIdCachePutCount());
        assertEquals("NaturalId Cache Queries", 1, stats.getNaturalIdQueryExecutionCount());
        markRollbackOnly(s);
    });
}
Also used : State(org.hibernate.test.cache.infinispan.functional.entities.State) ArrayList(java.util.ArrayList) List(java.util.List) Criteria(org.hibernate.Criteria) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Test(org.junit.Test)

Example 4 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 5 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 = getSession();
    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)174 Session (org.hibernate.Session)92 Test (org.junit.Test)69 Transaction (org.hibernate.Transaction)39 List (java.util.List)35 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)4 HibernateCallback (org.springframework.orm.hibernate3.HibernateCallback)4 HashSet (java.util.HashSet)3 Criterion (org.hibernate.criterion.Criterion)3 Example (org.hibernate.criterion.Example)3 Statistics (org.hibernate.stat.Statistics)3 Pager (org.hisp.dhis.common.Pager)3 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)2