Example usage for org.hibernate Query setCacheRegion

List of usage examples for org.hibernate Query setCacheRegion

Introduction

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

Prototype

Query<R> setCacheRegion(String cacheRegion);

Source Link

Document

Set the name of the cache region where query results should be cached (if cached at all).

Usage

From source file:br.gov.jfrj.siga.wf.dao.WfDao.java

License:Open Source License

/**
 * Pesquisa as configuraes que so semelhantes ao exemplo
 * /*from  www . ja va2 s.com*/
 * @param exemplo
 *            Uma configurao de exemplo para a pesquisa.
 * @return Lista de configuraes encontradas.
 */
public List<WfConfiguracao> consultar(final WfConfiguracao exemplo) {
    Query query = getSessao().getNamedQuery("consultarWfConfiguracoes");

    query.setLong("idTpConfiguracao", exemplo.getCpTipoConfiguracao().getIdTpConfiguracao());

    query.setCacheable(true);
    query.setCacheRegion(CACHE_QUERY_CONFIGURACAO);
    return query.list();
}

From source file:com.example.app.model.user.UserDAO.java

License:Open Source License

/**
 * Get a User for the given Principal./*from   w ww  . j  av a  2  s .co m*/
 *
 * @param principal the Principal to search for
 * @param status optional status to limit.
 *
 * @return a User, or null if none exists for the given Principal
 */
@Nullable
public User getUserForPrincipal(@Nullable Principal principal, @Nullable PrincipalStatus... status) {
    if (principal == null)
        return null;
    String queryString = "SELECT u FROM User u INNER JOIN u.principal p WHERE p = :principal";
    final boolean hasStatus = status != null && status.length > 0;
    if (hasStatus)
        queryString += " AND p.status IN (:status)";
    final Query query = getSession().createQuery(queryString);
    query.setCacheable(true);
    query.setCacheRegion(ProjectCacheRegions.MEMBER_QUERY);
    query.setParameter("principal", principal);
    if (hasStatus)
        query.setParameterList("status", status);
    return (User) query.uniqueResult();
}

From source file:com.example.app.model.user.UserDAO.java

License:Open Source License

/**
 * Get users matching the specified parameters.
 * All parameters are optional. If none are specified, an empty list is returned.
 *
 * @param firstName first name./*from  w  ww  .  ja va2  s.  c om*/
 * @param lastName last name.
 * @param email email address.
 * @param exclude optional exclusion collection.
 *
 * @return the user list.
 */
public List<User> getUsers(@Nullable String firstName, @Nullable String lastName, @Nullable String email,
        @Nullable Collection<User> exclude) {
    boolean hasFirst = !isEmptyString(firstName);
    boolean hasLast = !isEmptyString(lastName);
    boolean hasEmail = !isEmptyString(email);
    boolean hasExclude = exclude != null && !exclude.isEmpty();
    if (!hasFirst && !hasLast && !hasEmail)
        return new ArrayList<>();
    StringBuilder hql = new StringBuilder();
    hql.append("SELECT DISTINCT user FROM User user \n" + " INNER JOIN user.principal as p\n"
            + " LEFT JOIN p.contact as c \n");
    if (hasFirst || hasLast) {
        hql.append(" LEFT JOIN c.name as n\n");
    }
    if (hasEmail) {
        hql.append(" LEFT JOIN c.emailAddresses as ea\n");
    }
    hql.append(" WHERE (");
    if (hasFirst) {
        hql.append(" LOWER(n.first) LIKE :firstName");
    }
    if (hasLast) {
        if (hasFirst)
            hql.append(" OR");
        hql.append(" LOWER(n.last) LIKE :lastName");
    }
    if (hasEmail) {
        if (hasFirst || hasLast)
            hql.append(" OR");
        hql.append(" LOWER(ea.email) LIKE :email");
    }
    hql.append(')');

    if (hasExclude) {
        hql.append(" AND user NOT IN (:exclude)");
    }

    final Session session = getSession();
    final Query query = session.createQuery(hql.toString());
    query.setCacheable(true);
    query.setCacheRegion(ProjectCacheRegions.MEMBER_QUERY);

    if (hasFirst)
        query.setParameter("firstName", '%' + firstName.trim().toLowerCase() + '%');
    if (hasLast)
        query.setParameter("lastName", '%' + lastName.trim().toLowerCase() + '%');
    if (hasEmail)
        query.setParameter("email", '%' + email.trim().toLowerCase() + '%');
    if (hasExclude)
        query.setParameterList("exclude", exclude);

    @SuppressWarnings("unchecked")
    final List<User> list = query.list();
    return list;
}

From source file:com.example.app.profile.model.company.CompanyDAO.java

License:Open Source License

/**
 * Get users matching the specified parameters.
 * All parameters are optional. If none are specified, an empty list is returned.
 *
 * @param firstName first name./*from  w  w w . java  2 s.  c o m*/
 * @param lastName last name.
 * @param email email address.
 * @param exclude optional exclusion collection.
 * @param company the company
 *
 * @return the user list.
 */
public List<User> getUsers(@Nullable String firstName, @Nullable String lastName, @Nullable String email,
        @Nullable Collection<User> exclude, @Nonnull Company company) {
    boolean hasFirst = !isEmptyString(firstName);
    boolean hasLast = !isEmptyString(lastName);
    boolean hasEmail = !isEmptyString(email);
    boolean hasExclude = exclude != null && !exclude.isEmpty();
    if (!hasFirst && !hasLast && !hasEmail)
        return new ArrayList<>();
    StringBuilder hql = new StringBuilder();
    hql.append("SELECT DISTINCT user FROM Company ce\n" + " INNER JOIN ce.users user\n"
            + " INNER JOIN user.principal as p\n" + " LEFT JOIN p.contact as c \n");
    if (hasFirst || hasLast) {
        hql.append(" LEFT JOIN c.name as n\n");
    }
    if (hasEmail) {
        hql.append(" LEFT JOIN c.emailAddresses as ea\n");
    }
    hql.append(" WHERE (");
    if (hasFirst) {
        hql.append(" LOWER(n.first) LIKE :firstName");
    }
    if (hasLast) {
        if (hasFirst)
            hql.append(" OR");
        hql.append(" LOWER(n.last) LIKE :lastName");
    }
    if (hasEmail) {
        if (hasFirst || hasLast)
            hql.append(" OR");
        hql.append(" LOWER(ea.email) LIKE :email");
    }
    hql.append(")\n");

    if (hasExclude) {
        hql.append(" AND user NOT IN (:exclude)\n");
    }

    hql.append("AND ce.id = :ceid");

    final Session session = getSession();
    final Query query = session.createQuery(hql.toString());
    query.setCacheable(true);
    query.setCacheRegion(ProjectCacheRegions.MEMBER_QUERY);

    if (hasFirst)
        query.setParameter("firstName", '%' + firstName.trim().toLowerCase() + '%');
    if (hasLast)
        query.setParameter("lastName", '%' + lastName.trim().toLowerCase() + '%');
    if (hasEmail)
        query.setParameter("email", '%' + email.trim().toLowerCase() + '%');
    if (hasExclude)
        query.setParameterList("exclude", exclude);
    query.setParameter("ceid", company.getId());

    @SuppressWarnings("unchecked")
    final List<User> list = query.list();
    return list;
}

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  ww  .j  a va2s.  c o m
 * 
 * @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.liusoft.dlog4j.dao.DAO.java

License:Open Source License

/**
 * ()/*from w ww  . j a  va2s  . c om*/
 * @param hql
 * @param args
 * @return
 */
protected static List executeQueryCacheable(String cache_region, String hql, int fromIdx, int fetchCount,
        Object... args) {
    Session ssn = getSession();
    Query q = ssn.createQuery(hql).setReadOnly(true).setCacheable(true);
    if (cache_region != null)
        q.setCacheRegion(cache_region);
    for (int i = 0; i < args.length; i++) {
        q.setParameter(i, args[i]);
    }
    if (fromIdx > 0)
        q.setFirstResult(fromIdx);
    if (fetchCount > 0)
        q.setMaxResults(fetchCount);
    return q.list();
}

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

License:Open Source License

/**
 * /*from www .jav  a  2s  .c  om*/
 * <b></b>
 * @param hql
 * @param args
 * @return
 */
protected static Object uniqueResultCacheable(String cache_region, String hql, Object... args) {
    Session ssn = getSession();
    Query q = ssn.createQuery(hql).setReadOnly(true).setCacheable(true);
    if (cache_region != null)
        q.setCacheRegion(cache_region);
    for (int i = 0; i < args.length; i++) {
        q.setParameter(i, args[i]);
    }
    //q.setMaxResults(1);
    return q.uniqueResult();
}

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

License:Open Source License

/**
 * ()// ww  w  . j  a va 2 s . c  o m
 * @param hql
 * @param args
 * @return
 */
protected static List executeNamedQueryCacheable(String cache_region, String hql, int fromIdx, int fetchCount,
        Object... args) {
    Session ssn = getSession();
    Query q = ssn.getNamedQuery(hql).setReadOnly(true).setCacheable(true);
    if (cache_region != null)
        q.setCacheRegion(cache_region);
    for (int i = 0; i < args.length; i++) {
        q.setParameter(i, args[i]);
    }
    if (fromIdx > 0)
        q.setFirstResult(fromIdx);
    if (fetchCount > 0)
        q.setMaxResults(fetchCount);
    return q.list();
}

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

License:Open Source License

/**
 * // w w w.j a va  2 s .co m
 * <b></b>
 * @param hql
 * @param args
 * @return
 */
protected static Object namedUniqueResultCacheable(String cache_region, String hql, Object... args) {
    Session ssn = getSession();
    Query q = ssn.getNamedQuery(hql).setReadOnly(true).setCacheable(true);
    if (cache_region != null)
        q.setCacheRegion(cache_region);
    for (int i = 0; i < args.length; i++) {
        q.setParameter(i, args[i]);
    }
    //q.setMaxResults(1);
    return q.uniqueResult();
}

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

License:Open Source License

/**
 *  FIXME: photo/show.vm/*  www. j  a va  2s.  com*/
 * 
 * @param site
 * @param loginUser
 * @param month
 * @return
 */
public static int[] statCalendarPhotoCount(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];

    // 
    boolean is_owner = site.isOwner(user);
    StringBuffer hql = new StringBuffer(
            "SELECT j.createTime FROM PhotoBean AS j WHERE j.createTime>=:beginTime AND j.createTime<:endTime AND j.site.id=:site");
    if (!is_owner) {
        // 
        hql.append(" AND j.status=:status AND j.album.type=:album_type");
    }

    try {
        Session ssn = getSession();
        Query q = ssn.createQuery(hql.toString());
        q.setCacheable(true);
        q.setCacheRegion("query.photo_calendar");
        q.setTimestamp("beginTime", firstDate.getTime());
        q.setTimestamp("endTime", nextMonthFirstDate.getTime());
        q.setInteger("site", site.getId());
        if (!is_owner) {
            q.setInteger("status", PhotoBean.STATUS_NORMAL);
            q.setInteger("album_type", AlbumBean.TYPE_PUBLIC);
        }
        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;
    }
}