Example usage for org.hibernate Query setCacheable

List of usage examples for org.hibernate Query setCacheable

Introduction

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

Prototype

Query<R> setCacheable(boolean cacheable);

Source Link

Document

Enable/disable second level query (result) caching for this query.

Usage

From source file:com.hazelcast.hibernate.HibernateSlowTestSupport.java

License:Open Source License

protected DummyEntity executeQuery(SessionFactory factory, long id) {
    Session session = factory.openSession();
    try {//from ww  w  .j  av a 2s  .c o  m
        Query query = session.createQuery("from " + DummyEntity.class.getName() + " where id = " + id);
        query.setCacheable(true);
        return (DummyEntity) query.list().get(0);
    } finally {
        session.close();
    }
}

From source file:com.hazelcast.hibernate.HibernateStatisticsTestSupport.java

License:Open Source License

protected List<DummyEntity> executeQuery(SessionFactory factory) {
    Session session = factory.openSession();
    try {/* w w w .j  ava2  s  . c o m*/
        Query query = session.createQuery("from " + DummyEntity.class.getName());
        query.setCacheable(true);
        return query.list();
    } finally {
        session.close();
    }
}

From source file:com.hazelcast.hibernate.HibernateStatisticsTestSupport.java

License:Open Source License

protected void executeUpdateQuery(SessionFactory sf, String queryString) throws RuntimeException {
    Session session = null;//ww  w. j  a v a  2 s.c  o  m
    Transaction txn = null;
    try {
        session = sf.openSession();
        txn = session.beginTransaction();
        Query query = session.createQuery(queryString);
        query.setCacheable(true);
        query.executeUpdate();
        txn.commit();
    } catch (RuntimeException e) {
        txn.rollback();
        e.printStackTrace();
        throw e;
    } finally {
        session.close();
    }
}

From source file:com.hazelcast.hibernate.RegionFactoryDefaultTest.java

License:Open Source License

@Test
public void testSpecificQueryRegionEviction() {
    int entityCount = 10;
    insertDummyEntities(entityCount, 0);

    //miss 1 query list entities
    Session session = sf.openSession();/*from ww w  . ja va  2s. co  m*/
    Transaction txn = session.beginTransaction();
    Query query = session.createQuery("from " + DummyEntity.class.getName());
    query.setCacheable(true).setCacheRegion("newregionname");
    query.list();
    txn.commit();
    session.close();
    //query is cached

    //query is invalidated
    sf.getCache().evictQueryRegion("newregionname");

    //miss 1 query
    session = sf.openSession();
    txn = session.beginTransaction();
    query = session.createQuery("from " + DummyEntity.class.getName());
    query.setCacheable(true);
    query.list();
    txn.commit();
    session.close();

    assertEquals(0, sf.getStatistics().getQueryCacheHitCount());
    assertEquals(2, sf.getStatistics().getQueryCacheMissCount());
}

From source file:com.jdon.persistence.hibernate.HibernateTemplate.java

License:Apache License

/**
 * Prepare the given Query object, applying cache settings and/or a
 * transaction timeout.//from   w w w  .  j a v a2  s.  c  om
 * 
 * @param queryObject
 *            the Query object to prepare
 * @see #setCacheQueries
 * @see #setQueryCacheRegion
 * @see SessionProviderHolder#applyTransactionTimeout
 */
protected void prepareQuery(Query queryObject) {
    if (isCacheQueries()) {
        queryObject.setCacheable(true);
        if (getQueryCacheRegion() != null) {
            queryObject.setCacheRegion(getQueryCacheRegion());
        }
    }
    if (getFetchSize() > 0) {
        queryObject.setFetchSize(getFetchSize());
    }
    if (getMaxResults() > 0) {
        queryObject.setMaxResults(getMaxResults());
    }

    if (getFirstResult() > 0) {
        queryObject.setFirstResult(getFirstResult());
    }

}

From source file:com.lighting.platform.base.dao.SimpleHibernateDao.java

License:Apache License

/**
 * Querydistinct transformer.// w  w  w.  ja  v  a  2 s.  co  m
 * ?HQL??, ?distinct?.
 */
public Query distinct(Query query) {
    query.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    return query.setCacheable(cacheable());
}

From source file:com.lighting.platform.base.dao.SimpleHibernateDao.java

License:Apache License

/**
 * ?HQL?Query.//ww w  . j av a  2  s.  co m
 * find()???.
 * 
 * @param values ????,?.
 */
public Query createQuery(final String queryString, final Object... values) {
    AssertUtils.hasText(queryString, "queryString?");
    Query query = getSession().createQuery(queryString);
    if (values != null) {
        for (int i = 0; i < values.length; i++) {
            query.setParameter(i, values[i]);
        }
    }
    return query.setCacheable(cacheable());
}

From source file:com.lighting.platform.base.dao.SimpleHibernateDao.java

License:Apache License

/**
 * ?HQL?Query./*from  w ww .  j  av a2 s  .co  m*/
 * find()???.
 * 
 * @param values ???,??.
 */
public Query createQuery(final String queryString, final Map<String, ?> values) {
    AssertUtils.hasText(queryString, "queryString?");
    Query query = getSession().createQuery(queryString);
    if (values != null) {
        query.setProperties(values);
    }
    return query.setCacheable(cacheable());
}

From source file:com.liusoft.dlog4j.dao.DiaryDAO.java

License:Open Source License

/**
 * /*from  ww w.  j av  a 2  s.c o m*/
 * 
 * @param site
 * @param loginUser
 * @param month
 * @return
 */
public static int[] statCalendarLogs(SiteBean site, SessionUserObject user, Calendar month) {
    Calendar firstDate = (Calendar) month.clone();
    firstDate.set(Calendar.DATE, 1);
    DateUtils.resetTime(firstDate);
    Calendar nextMonthFirstDate = (Calendar) firstDate.clone();
    nextMonthFirstDate.add(Calendar.MONTH, 1);

    // 
    Calendar tempCal = (Calendar) nextMonthFirstDate.clone();
    tempCal.add(Calendar.DATE, -1);
    int dateCount = tempCal.get(Calendar.DATE);
    int[] logCounts = new int[dateCount + 1];

    // 

    StringBuffer hql = new StringBuffer(
            "SELECT j.writeTime FROM DiaryBean AS j WHERE j.writeTime>=:beginTime AND j.writeTime<:endTime AND j.status=:status AND j.site.id=:site");
    if (!site.isOwner(user)) {
        // 
        hql.append(" AND (j.catalog.type<>:cat_type");
        if (user != null)
            hql.append(
                    " OR (j.catalog.type=:cat_type AND j.catalog.id IN (SELECT p.key.catalog FROM CatalogPermBean AS p WHERE p.key.user=:user))");
        hql.append(')');
    }

    Session ssn = getSession();

    try {
        Query q = ssn.createQuery(hql.toString()).setCacheable(true);
        q.setTimestamp("beginTime", firstDate.getTime());
        q.setTimestamp("endTime", nextMonthFirstDate.getTime());
        q.setInteger("status", DiaryBean.STATUS_NORMAL);
        q.setInteger("site", site.getId());
        if (!site.isOwner(user)) {
            q.setInteger("cat_type", CatalogBean.TYPE_OWNER);
            if (user != null)
                q.setInteger("user", user.getId());
        }
        q.setCacheable(true).setCacheRegion("query.diary_calendar");
        int total = 0;
        Iterator logs = q.list().iterator();
        while (logs.hasNext()) {
            tempCal.setTime((Date) logs.next());
            int date = tempCal.get(Calendar.DATE);
            logCounts[date]++;
            total++;
        }

        logCounts[0] = total;

        return logCounts;
    } finally {
        hql = null;
        firstDate = null;
        nextMonthFirstDate = null;
        tempCal = null;
    }
}

From source file:com.liusoft.dlog4j.dao.PhotoDAO.java

License:Open Source License

/**
 * /*from w ww  .j a v  a  2 s. c o  m*/
 * 
 * @param site
 * @param user
 * @param album_id
 * @param month_stamp
 *            ,20050620056
 * @param fromIdx
 * @param count
 * @return
 * @see com.liusoft.dlog4j.velocity.DLOG_VelocityTool#list_photos(SiteBean,
 *      int, int)
 */
public static List listPhotos(int album_id, int month_stamp, int date, int fromIdx, int count) {
    StringBuffer hql = new StringBuffer("FROM PhotoOutlineBean AS p WHERE 1=1");
    if (album_id > 0)
        hql.append(" AND (p.album.id=:album OR p.album.parent.id=:album)");
    else {
        // hql.append(" AND (TO_DAYS(NOW()) - TO_DAYS(p.site.createTime) >
        // 2)");
    }
    if (month_stamp > 190000 && month_stamp < 209912) {
        hql.append(" AND p.year=:year AND p.month=:month");
    }
    hql.append(" AND p.status<>:hidden_status AND p.album.type=:owner_album");
    if (date > 0) {
        hql.append(" AND p.date=:date");
    }
    hql.append(" AND p.site.status=:site_status ORDER BY p.id DESC");
    Session ssn = getSession();
    try {
        Query q = ssn.createQuery(hql.toString());
        q.setCacheable(true).setCacheRegion("query.new_photos");
        if (album_id > 0)
            q.setInteger("album", album_id);
        if (month_stamp > 190000 && month_stamp < 209912) {
            q.setInteger("year", month_stamp / 100);
            q.setInteger("month", month_stamp % 100);
        }
        q.setInteger("hidden_status", PhotoBean.STATUS_PRIVATE);
        q.setInteger("owner_album", AlbumBean.TYPE_PUBLIC);
        if (date > 0) {
            q.setInteger("date", date);
        }
        q.setInteger("site_status", SiteBean.STATUS_NORMAL);
        q.setFirstResult(fromIdx);
        q.setMaxResults(count);
        return q.list();
    } finally {
        hql = null;
    }
}