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:org.beanfuse.query.hibernate.HibernateQuerySupport.java

License:Open Source License

/**
 * /*from  www .  j a  va 2  s  . c  o m*/
 * 
 * @param query
 * @param hibernateSession
 * @return
 */
public static int count(final AbstractQuery query, final Session hibernateSession) {
    final String countQueryStr = query.toCountString();
    if (StringUtils.isEmpty(countQueryStr)) {
        Query hibernateQuery = null;
        if (query instanceof EntityQuery) {
            hibernateQuery = hibernateSession.createQuery(query.toQueryString());
        } else {
            hibernateQuery = hibernateSession.createSQLQuery(query.toQueryString());
        }
        if (query.isCacheable()) {
            hibernateQuery.setCacheable(query.isCacheable());
        }
        setParameter(hibernateQuery, query.getParams());
        return hibernateQuery.list().size();
    } else {
        Query countQuery = null;
        if (query instanceof EntityQuery) {
            countQuery = hibernateSession.createQuery(countQueryStr);
        } else {
            countQuery = hibernateSession.createSQLQuery(countQueryStr);
        }
        if (query.isCacheable()) {
            countQuery.setCacheable(query.isCacheable());
        }
        setParameter(countQuery, query.getParams());
        final Number count = (Number) (countQuery.uniqueResult());
        if (null == count) {
            return 0;
        } else {
            return count.intValue();
        }
    }
}

From source file:org.beanfuse.query.hibernate.HibernateQuerySupport.java

License:Open Source License

/**
 * /*from  w  w  w. ja  v  a 2 s .  c o m*/
 * 
 * @param query
 * @param hibernateSession
 * @return
 */
public static List find(final AbstractQuery query, final Session hibernateSession) {
    Query hibernateQuery = null;
    if (query instanceof EntityQuery) {
        hibernateQuery = hibernateSession.createQuery(query.toQueryString());
    } else {
        hibernateQuery = hibernateSession.createSQLQuery(query.toQueryString());
    }
    if (query.isCacheable()) {
        hibernateQuery.setCacheable(query.isCacheable());
    }
    setParameter(hibernateQuery, query.getParams());
    if (null == query.getLimit()) {
        return hibernateQuery.list();
    } else {
        final PageLimit limit = query.getLimit();
        hibernateQuery.setFirstResult((limit.getPageNo() - 1) * limit.getPageSize())
                .setMaxResults(limit.getPageSize());
        return hibernateQuery.list();
    }
}

From source file:org.beanfuse.security.dao.AuthorityDaoHibernate.java

License:Apache License

/**
 * ?//from  w w w  . ja va  2 s.c  o m
 */
public List getResources(Group group) {
    String hql = "select distinct m from Group as r join r.authorities as a"
            + " join a.resource as m where  r.id = :groupId and m.enabled = true";
    Query query = getSession().createQuery(hql);
    query.setParameter("groupId", group.getId());
    query.setCacheable(true);
    return query.list();
}

From source file:org.beanfuse.security.dao.AuthorityDaoHibernate.java

License:Apache License

/**
 * ?id//  w ww .ja v a2 s .  c  o  m
 */
public Set getResourceIds(Group group) {
    String hql = "select m.id from Group as r join r.authorities as a"
            + " join a.resource as m where  r.id = :groupId and m.enabled = true";
    Query query = getSession().createQuery(hql);
    query.setParameter("groupId", group.getId());
    query.setCacheable(true);
    return new HashSet(query.list());
}

From source file:org.beanfuse.security.menu.dao.MenuAuthorityDaoHibernate.java

License:Apache License

/**
 * ?//from   w w  w  .ja v  a 2  s .co  m
 */
public List getMenus(MenuProfile profile, Group group, int depth, String ancestorCode) {
    StringBuilder hql = new StringBuilder(" select m from MenuAuthority a join a.group as r");
    hql.append(" join a.menu as m where r = :group" + " and m.enabled=true ");
    if (null != profile) {
        hql.append(" and m.profile=:profile");
    }
    if (StringUtils.isNotEmpty(ancestorCode)) {
        hql.append(" and m.code like :ancestorCode and length(m.code)>:ancestorCodeLength");
    }
    if (depth > 0) {
        hql.append(" and length(m.code)/2 <= :depth");
    }
    Query query = getSession().createQuery(hql.toString());
    query.setParameter("group", group);

    if (StringUtils.isNotEmpty(ancestorCode)) {
        query.setParameter("ancestorCode", ancestorCode + "%");
        query.setParameter("ancestorCodeLength", new Long(ancestorCode.length()));
    }
    if (depth > 0) {
        query.setParameter("depth", new Long(depth));
    }
    if (null != profile) {
        query.setParameter("profile", profile);
    }
    query.setCacheable(true);
    return query.list();
}

From source file:org.beanfuse.security.menu.dao.MenuDaoHibernate.java

License:Apache License

private List getDescendants(String ancestorCode, int depth, Boolean isEnabled) {
    String hql = "select  a from  Menu as a where" + "      a.code like :ancestorCode and"
            + "      length(a.code) > :ancestorCodeLength and"
            + "      (:depth=-1 or length(a.code)/2 <= :depth)";
    if (null != isEnabled) {
        hql += " and a.enabled=" + isEnabled;
    }//from  w w  w.j  a v  a2  s.  co m
    hql += " order by a.code";
    Query query = getSession().createQuery(hql);
    query.setParameter("ancestorCode", ancestorCode + "%");
    query.setParameter("depth", new Integer(depth));
    query.setParameter("ancestorCodeLength", new Long(ancestorCode.length() + 1));
    query.setCacheable(true);
    return query.list();
}

From source file:org.beanfuse.security.menu.dao.MenuDaoHibernate.java

License:Apache License

private List getChildren(String parentCode, Boolean isEnabled) {
    String hql = "select from  Menu as a where a.code like :parentCode"
            + "      and length(a.code) - parentCodeLength<=2" + "       and length(a.code) >parentCodeLength";
    if (null != isEnabled) {
        hql += " and a.enabled=" + isEnabled;
    }//  w  w  w  .  j ava  2  s .c  o m
    hql += " order by a.code";
    Query query = getSession().createQuery(hql);
    query.setParameter("parentCode", parentCode + "%");
    query.setParameter("parentCodeLength", new Integer(parentCode.length() + 1));
    query.setCacheable(true);
    return query.list();
}

From source file:org.beangle.commons.orm.hibernate.HibernateEntityDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public <T> List<T> getAll(Class<T> clazz) {
    String hql = "from " + Model.getEntityType(clazz).getEntityName();
    Query query = getSession().createQuery(hql);
    query.setCacheable(true);
    return query.list();
}

From source file:org.beangle.commons.orm.hibernate.HibernateEntityDao.java

License:Open Source License

@SuppressWarnings({ "unchecked" })
public <T> List<T> searchNamedQuery(final String queryName, final Map<String, Object> params,
        boolean cacheable) {
    Query query = getSession().getNamedQuery(queryName);
    query.setCacheable(cacheable);
    return QuerySupport.setParameter(query, params).list();
}

From source file:org.beangle.commons.orm.hibernate.HibernateEntityDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public <T> List<T> searchHQLQuery(String hql, final Map<String, Object> params, boolean cacheable) {
    Query query = getSession().createQuery(hql);
    query.setCacheable(cacheable);
    return QuerySupport.setParameter(query, params).list();
}