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