Example usage for org.hibernate Criteria setCacheable

List of usage examples for org.hibernate Criteria setCacheable

Introduction

In this page you can find the example usage for org.hibernate Criteria setCacheable.

Prototype

public Criteria setCacheable(boolean cacheable);

Source Link

Document

Enable caching of this query result, provided query caching is enabled for the underlying session factory.

Usage

From source file:org.iesc.flightws.domain.PlaneDaoBase.java

/**
 * @see org.iesc.flightws.domain.PlaneDao#loadAll(int, int, int)
 *///ww w . ja  v a2  s . c  om
public java.util.Collection loadAll(final int transform, final int pageNumber, final int pageSize) {
    try {
        final org.hibernate.Criteria criteria = this.getSession(false)
                .createCriteria(org.iesc.flightws.domain.PlaneImpl.class);
        criteria.setCacheable(true);
        if (pageNumber > 0 && pageSize > 0) {
            criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
            criteria.setMaxResults(pageSize);
        }
        final java.util.Collection results = criteria.list();
        this.transformEntities(transform, results);
        return results;
    } catch (org.hibernate.HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}

From source file:org.iesc.flightws.domain.PriceDaoBase.java

/**
 * @see org.iesc.flightws.domain.PriceDao#loadAll(int, int, int)
 *///from   w ww .  j a va2s  .c o  m
public java.util.Collection loadAll(final int transform, final int pageNumber, final int pageSize) {
    try {
        final org.hibernate.Criteria criteria = this.getSession(false)
                .createCriteria(org.iesc.flightws.domain.PriceImpl.class);
        criteria.setCacheable(true);
        if (pageNumber > 0 && pageSize > 0) {
            criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
            criteria.setMaxResults(pageSize);
        }
        final java.util.Collection results = criteria.list();
        this.transformEntities(transform, results);
        return results;
    } catch (org.hibernate.HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}

From source file:org.iesc.flightws.domain.UserDaoBase.java

/**
 * @see org.iesc.flightws.domain.UserDao#loadAll(int, int, int)
 *///from   w  w w  .ja v  a2 s .  com
public java.util.Collection loadAll(final int transform, final int pageNumber, final int pageSize) {
    try {
        final org.hibernate.Criteria criteria = this.getSession(false)
                .createCriteria(org.iesc.flightws.domain.UserImpl.class);
        criteria.setCacheable(true);
        if (pageNumber > 0 && pageSize > 0) {
            criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
            criteria.setMaxResults(pageSize);
        }
        final java.util.Collection results = criteria.list();
        this.transformEntities(transform, results);
        return results;
    } catch (org.hibernate.HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}

From source file:org.inbio.m3s.dao.core.impl.KeywordDAOImpl.java

License:Open Source License

public Keyword findByName(final String keywordName) throws IllegalArgumentException {
    logger.debug("findAllByName with [" + keywordName + "] ");
    HibernateTemplate template = getHibernateTemplate();
    return (Keyword) template.execute(new HibernateCallback() {
        public Object doInHibernate(Session session) {
            Criteria criteria = session.createCriteria(Keyword.class, "k");
            criteria.createAlias("translations", "t");
            criteria.add(Restrictions.ilike("t.name", keywordName));

            criteria.setCacheable(true);

            return criteria.uniqueResult();

        }/*from ww  w  .  ja v a 2  s. c o  m*/
    });
}

From source file:org.inbio.m3s.dao.core.impl.KeywordDAOImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Keyword> findAllByPartialNamePaginated(final String partialKeywrod, final int maxResults) {
    logger.debug("findAllByPartialNamePaginatedUncaseSensitive with[" + partialKeywrod + "]");

    HibernateTemplate template = getHibernateTemplate();
    return (List<Keyword>) template.execute(new HibernateCallback() {
        public Object doInHibernate(Session session) {

            Criteria criteria = session.createCriteria(Keyword.class, "k");
            criteria.createAlias("translations", "t");
            criteria.add(Restrictions.ilike("t.name", partialKeywrod));

            criteria.setFirstResult(0);/*w w  w .ja  va  2s  . c  o  m*/
            criteria.setMaxResults(maxResults);
            criteria.setCacheable(true);

            return criteria.list();

        }
    });

}

From source file:org.infinispan.test.hibernate.cache.commons.functional.cluster.NaturalIdInvalidationTest.java

License:LGPL

private void getCitizenWithCriteria(SessionFactory sf) throws Exception {
    withTxSession(sf, s -> {//  w  w  w.  j  ava  2  s.  c  om
        State france = getState(s, "Ile de France");
        Criteria criteria = s.createCriteria(Citizen.class);
        criteria.add(Restrictions.naturalId().set("ssn", "1234").set("state", france));
        criteria.setCacheable(true);
        criteria.list();
    });
}

From source file:org.infinispan.test.hibernate.cache.commons.functional.cluster.NaturalIdInvalidationTest.java

License:LGPL

private void deleteCitizenWithCriteria(SessionFactory sf) throws Exception {
    withTxSession(sf, s -> {/*from  ww w. ja v a 2 s  . c  o m*/
        State france = getState(s, "Ile de France");
        Criteria criteria = s.createCriteria(Citizen.class);
        criteria.add(Restrictions.naturalId().set("ssn", "1234").set("state", france));
        criteria.setCacheable(true);
        Citizen c = (Citizen) criteria.uniqueResult();
        s.delete(c);
    });
}

From source file:org.infinispan.test.hibernate.cache.commons.functional.cluster.NaturalIdInvalidationTest.java

License:LGPL

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

From source file:org.infinispan.test.hibernate.cache.commons.functional.ReadWriteTest.java

License:LGPL

@Test
public void testNaturalIdCached() throws Exception {
    saveSomeCitizens();//ww w. j av a 2 s . co m

    // 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());

        // first query
        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());

        // query a second time - result should be cached in session
        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());

        // cleanup
        markRollbackOnly(s);
    });
}

From source file:org.informea.odata.data.db.DatabaseDataProvider.java

License:Open Source License

/**
 * Count the primary entities by issuing "SELECT COUNT(*) ... "
 * @param entityClass Class to count//from w  w w.j a v  a2s . co m
 * @param q Filtering query, optional
 * @return Count of rows
 */
@SuppressWarnings("rawtypes")
@Override
public Integer countPrimaryEntities(Class entityClass, QueryInfo q) {
    Criteria query = session.createCriteria(entityClass);
    query = query.setProjection(Projections.rowCount());
    return ((Long) query.setCacheable(true).uniqueResult()).intValue();
}