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