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.springframework.orm.hibernate4.HibernateTemplateTests.java

License:Apache License

@Test
public void testLoadAllWithCacheableAndCacheRegion() {
    Criteria criteria = mock(Criteria.class);
    List list = new ArrayList();
    given(session.createCriteria(TestBean.class)).willReturn(criteria);
    given(criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)).willReturn(criteria);
    given(criteria.setCacheable(true)).willReturn(criteria);
    given(criteria.setCacheRegion("myCacheRegion")).willReturn(criteria);
    given(criteria.list()).willReturn(list);

    hibernateTemplate.setCacheQueries(true);
    hibernateTemplate.setQueryCacheRegion("myCacheRegion");
    List result = hibernateTemplate.loadAll(TestBean.class);
    assertTrue("Correct result", result == list);
    verify(criteria).setCacheable(true);
    verify(criteria).setCacheRegion("myCacheRegion");
}

From source file:org.squale.jraf.provider.persistence.hibernate.AbstractDAOImpl.java

License:Open Source License

/**
 * Execute un find a partir d'un objet exemple
 * /*w w w.j  a  v a 2  s. c om*/
 * @param session session de persistance
 * @param bo businness object exemple
 * @param nbLignes nombre de lignes
 * @param indexDepart index de depart
 * @param ignoreCase ignore la casse
 * @param likeMode mode like
 * @param cache true si les elements retournes sont mis en cache, false sinon
 * @return liste d'objets retrouves
 * @throws JrafDaoException
 */
public List findByExample(ISession session, Object bo, int nbLignes, int indexDepart, boolean ignoreCase,
        boolean likeMode, boolean cache, int matchMode) throws JrafDaoException {

    SessionImpl sessionImpl = (SessionImpl) session;
    Criteria crit = sessionImpl.getSession().createCriteria(bo.getClass());

    Example example = Example.create(bo);

    // ignore la casse
    if (ignoreCase) {
        example.ignoreCase();
    }

    // mode like
    if (likeMode) {
        switch (matchMode) {
        case 0:
            example.enableLike(MatchMode.START);
            break;
        case 1:
            example.enableLike(MatchMode.END);
            break;
        default:
            example.enableLike(MatchMode.ANYWHERE);
            break;
        }
    }

    // ajout de l'example
    crit.add(example);

    if (indexDepart > -1) {
        crit.setFirstResult(indexDepart);
    }

    if (nbLignes > 0) {
        crit.setMaxResults(nbLignes);
    }

    crit.setCacheable(cache);

    List l = null;
    try {
        l = crit.list();
    } catch (HibernateException e) {
        throwDAOException(e, "findByExample");

    }
    return l;

}

From source file:org.tonguetied.administration.ServerDataRepositoryImpl.java

License:Apache License

public ServerData getServerData(final Long id) {
    Criteria criteria = getSession().createCriteria(ServerData.class);
    criteria.add(idEq(id));//from  ww w.  j a v  a2  s .co  m
    criteria.setCacheable(true);
    return (ServerData) criteria.uniqueResult();
}

From source file:org.tonguetied.keywordmanagement.BundleRepositoryImpl.java

License:Apache License

public Bundle getBundle(final Long id) {
    Criteria criteria = getSession().createCriteria(Bundle.class);
    criteria.add(idEq(id));//from ww  w . ja  v a 2s.c  o  m
    criteria.setCacheable(true);
    return (Bundle) criteria.uniqueResult();
}

From source file:org.tonguetied.keywordmanagement.BundleRepositoryImpl.java

License:Apache License

public Bundle getBundleByName(final String name) {
    Criteria criteria = getSession().createCriteria(Bundle.class);
    criteria.add(eq(FIELD_NAME, name));/*from w w  w  . j a va  2s . c o m*/
    criteria.setCacheable(true);
    return (Bundle) criteria.uniqueResult();
}

From source file:org.tonguetied.keywordmanagement.BundleRepositoryImpl.java

License:Apache License

public Bundle getBundleByResourceName(final String resourceName) {
    Criteria criteria = getSession().createCriteria(Bundle.class);
    criteria.add(eq(FIELD_RESOURCE_NAME, resourceName));
    criteria.setCacheable(true);
    return (Bundle) criteria.uniqueResult();
}

From source file:org.tonguetied.keywordmanagement.CountryRepositoryImpl.java

License:Apache License

public Country getCountry(final Long id) {
    Criteria criteria = getSession().createCriteria(Country.class);
    criteria.add(idEq(id));/*w  w  w .  j av a 2  s.c om*/
    criteria.setCacheable(true);
    return (Country) criteria.uniqueResult();
}

From source file:org.tonguetied.keywordmanagement.CountryRepositoryImpl.java

License:Apache License

public Country getCountry(final CountryCode code) {
    Criteria criteria = getSession().createCriteria(Country.class);
    criteria.add(eq(FIELD_CODE, code));//from  www.j av a 2  s.  c om
    criteria.setCacheable(true);
    return (Country) criteria.uniqueResult();
}

From source file:org.tonguetied.keywordmanagement.LanguageRepositoryImpl.java

License:Apache License

public Language getLanguage(final Long id) {
    Criteria criteria = getSession().createCriteria(Language.class);
    criteria.add(idEq(id));//from w  w  w . j ava 2 s  .c om
    criteria.setCacheable(true);
    return (Language) criteria.uniqueResult();
}

From source file:org.tonguetied.keywordmanagement.LanguageRepositoryImpl.java

License:Apache License

public Language getLanguage(final LanguageCode code) {
    Criteria criteria = getSession().createCriteria(Language.class);
    criteria.add(eq(FIELD_CODE, code));// w w w. j  a va  2  s.c o m
    criteria.setCacheable(true);
    return (Language) criteria.uniqueResult();
}