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.picketlink.idm.impl.store.hibernate.HibernateIdentityStoreImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public org.hibernate.Query prepareIdentityObjectQuery(IdentityStoreInvocationContext ctx,
        IdentityObject identity, IdentityObjectRelationshipType relationshipType,
        Collection<IdentityObjectType> excludes, boolean parent, IdentityObjectSearchCriteria criteria,
        boolean count) throws IdentityException {
    //TODO:test/*from  ww  w  .  jav a  2 s.  c o  m*/

    HibernateIdentityObject hibernateObject = safeGet(ctx, identity);
    HibernateRealm realm = getRealm(getHibernateSession(ctx), ctx);

    boolean orderByName = false;
    boolean ascending = true;

    if (criteria != null && criteria.isSorted()) {
        orderByName = true;
        ascending = criteria.isAscending();
    }

    org.hibernate.Query q = null;

    try {

        StringBuilder hqlString = new StringBuilder("");

        if (parent) {

            if (count) {
                hqlString.append(
                        "select count(distinct ior.toIdentityObject) from HibernateIdentityObjectRelationship ior where ");

            } else {
                hqlString.append(
                        "select distinct ior.toIdentityObject from HibernateIdentityObjectRelationship ior where ");
            }

            hqlString.append(
                    "ior.toIdentityObject.realm = :realm and ior.fromIdentityObject.realm = :realm and ");

            if (relationshipType != null) {

                hqlString.append(
                        "ior.toIdentityObject.name like :nameFilter and ior.type.name = :relType and ior.fromIdentityObject = :identity");
            } else {
                hqlString.append(
                        "ior.toIdentityObject.name like :nameFilter and ior.fromIdentityObject = :identity");
            }

            if (excludes != null && excludes.size() > 0) {

                int i = 0;
                for (IdentityObjectType exclude : excludes) {
                    hqlString.append(" and ior.toIdentityObject.identityType.id <> ").append(":exclude" + i++);
                }
            }

            if (orderByName) {
                hqlString.append(" order by ior.toIdentityObject.name");
                if (ascending) {
                    hqlString.append(" asc");
                }
            }
        } else {

            if (count) {
                hqlString.append(
                        "select count(distinct ior.fromIdentityObject) from HibernateIdentityObjectRelationship ior where ");

            } else {
                hqlString.append(
                        "select distinct ior.fromIdentityObject from HibernateIdentityObjectRelationship ior where ");
            }

            hqlString.append(
                    "ior.toIdentityObject.realm = :realm and ior.fromIdentityObject.realm = :realm and ");

            if (relationshipType != null) {
                hqlString.append(
                        "ior.fromIdentityObject.name like :nameFilter and ior.type.name = :relType and ior.toIdentityObject = :identity");
            } else {
                hqlString.append(
                        "ior.fromIdentityObject.name like :nameFilter and ior.toIdentityObject = :identity");
            }

            if (excludes != null && excludes.size() > 0) {
                int i = 0;
                for (IdentityObjectType exclude : excludes) {
                    hqlString.append(" and ior.fromIdentityObject.identityType.id <> ")
                            .append(":exclude" + i++);
                }
            }

            if (orderByName) {
                hqlString.append(" order by ior.fromIdentityObject.name");
                if (ascending) {
                    hqlString.append(" asc");
                }
            }
        }

        q = getHibernateSession(ctx).createQuery(hqlString.toString()).setParameter("identity", hibernateObject)
                .setParameter("realm", realm).setCacheable(true);

        if (relationshipType != null) {
            q.setParameter("relType", relationshipType.getName());
        }

        if (criteria != null && criteria.getFilter() != null) {
            q.setParameter("nameFilter", criteria.getFilter().replaceAll("\\*", "%"));
        } else {
            q.setParameter("nameFilter", "%");
        }

        if (excludes != null && excludes.size() > 0) {
            int i = 0;
            for (IdentityObjectType exclude : excludes) {
                HibernateIdentityObjectType exType = getHibernateIdentityObjectType(ctx, exclude);
                q.setParameter("exclude" + i++, exType.getId());
            }
        }
        if (criteria != null && criteria.isPaged() && !criteria.isFiltered()) {
            q.setFirstResult(criteria.getFirstResult());
            if (criteria.getMaxResults() > 0) {
                q.setMaxResults(criteria.getMaxResults());
            }
        }

        q.setCacheable(true);
    } catch (Exception e) {
        if (log.isLoggable(Level.FINER)) {
            log.log(Level.FINER, "Exception occurred: ", e);
        }

        throw new IdentityException("Cannot prepare hibernate query", e);
    }

    return q;
}

From source file:org.sakaiproject.component.app.messageforums.MessageForumsForumManagerImpl.java

License:Educational Community License

public List<Attachment> getTopicAttachments(final Long topicId) {
    if (topicId == null) {
        throw new IllegalArgumentException("Null Argument topicId");
    }//from w ww .  j a  v  a 2 s .c  o m
    HibernateCallback hcb = new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query q = session.getNamedQuery("findTopicAttachments");
            q.setCacheable(true);
            q.setParameter("topic", topicId, Hibernate.LONG);
            return q.list();
        }
    };
    return (List<Attachment>) getHibernateTemplate().executeFind(hcb);
}

From source file:org.sakaiproject.component.common.manager.TypeManagerImpl.java

License:Educational Community License

public Type getType(final String uuid) {
    if (uuid == null || uuid.length() < 1) {
        throw new IllegalArgumentException("uuid");
    }/*from   w ww. ja v  a  2s  .c om*/

    final HibernateCallback hcb = new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query q = session.getNamedQuery(FIND_TYPE_BY_UUID);
            q.setString(UUID, uuid);
            q.setCacheable(cacheFindTypeByUuid);
            q.setCacheRegion(Type.class.getCanonicalName());
            return q.uniqueResult();
        }
    };
    Type type = (Type) getHibernateTemplate().execute(hcb);
    return type;
}

From source file:org.sakaiproject.component.common.manager.TypeManagerImpl.java

License:Educational Community License

public Type getType(final String authority, final String domain, final String keyword) {
    // validation
    if (authority == null || authority.length() < 1)
        throw new IllegalArgumentException("authority");
    if (domain == null || domain.length() < 1)
        throw new IllegalArgumentException("domain");
    if (keyword == null || keyword.length() < 1)
        throw new IllegalArgumentException("keyword");

    final HibernateCallback hcb = new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query q = session.getNamedQuery(FIND_TYPE_BY_TUPLE);
            q.setString(AUTHORITY, authority);
            q.setString(DOMAIN, domain);
            q.setString(KEYWORD, keyword);
            q.setCacheable(cacheFindTypeByTuple);
            q.setCacheRegion(Type.class.getCanonicalName());
            return q.uniqueResult();
        }/*from  w ww.  j  a  va2 s  .co  m*/
    };
    Type type = (Type) getHibernateTemplate().execute(hcb);
    return type;
}

From source file:org.sakaiproject.component.common.type.TypeManagerImpl.java

License:Educational Community License

/**
 * @see org.sakaiproject.service.common.type.TypeManager#getType(java.lang.String)
 *//*  w  w  w .jav a2  s. c  om*/
public Type getType(final String uuid) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("getType(String " + uuid + ")");
    }
    if (uuid == null || uuid.length() < 1) {
        throw new IllegalArgumentException("uuid");
    }

    final HibernateCallback hcb = new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query q = session.getNamedQuery(FINDTYPEBYUUID);
            q.setString(UUID, uuid);
            q.setCacheable(cacheFindTypeByUuid);
            q.setCacheRegion(Type.class.getCanonicalName());
            return q.uniqueResult();
        }
    };
    Type type = (Type) getHibernateTemplate().execute(hcb);
    return type;
}

From source file:org.sakaiproject.component.common.type.TypeManagerImpl.java

License:Educational Community License

/**
 * @see org.sakaiproject.service.common.type.TypeManager#getType(java.lang.String, java.lang.String, java.lang.String)
 *//*  w w w .  j  av  a 2  s.c  o m*/
public Type getType(final String authority, final String domain, final String keyword) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("getType(String " + authority + ", String " + domain + ", String " + keyword + ")");
    }
    // validation
    if (authority == null || authority.length() < 1)
        throw new IllegalArgumentException("authority");
    if (domain == null || domain.length() < 1)
        throw new IllegalArgumentException("domain");
    if (keyword == null || keyword.length() < 1)
        throw new IllegalArgumentException("keyword");

    final HibernateCallback hcb = new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query q = session.getNamedQuery(FINDTYPEBYTUPLE);
            q.setString(AUTHORITY, authority);
            q.setString(DOMAIN, domain);
            q.setString(KEYWORD, keyword);
            q.setCacheable(cacheFindTypeByTuple);
            q.setCacheRegion(Type.class.getCanonicalName());
            return q.uniqueResult();
        }
    };
    Type type = (Type) getHibernateTemplate().execute(hcb);
    return type;
}

From source file:org.sakaiproject.coursemanagement.impl.CourseManagementServiceHibernateImpl.java

License:Educational Community License

public List<AcademicSession> getAcademicSessions() {
    return (List<AcademicSession>) getHibernateTemplate().execute(new HibernateCallback() {
        @Override//from  w  w w .  j  av  a  2  s .  c  o m
        public List<AcademicSession> doInHibernate(Session session) {
            Query query = session.getNamedQuery("findAcademicSessions");
            query.setCacheable(true);
            return query.list();
        }
    });

}

From source file:org.sakaiproject.coursemanagement.impl.CourseManagementServiceHibernateImpl.java

License:Educational Community License

public List<AcademicSession> getCurrentAcademicSessions() {
    return (List<AcademicSession>) getHibernateTemplate().execute(new HibernateCallback() {
        @Override/*from  w  w  w.  j  av  a 2 s  . c  o  m*/
        public List<AcademicSession> doInHibernate(Session session) {
            Query query = session.getNamedQuery("findCurrentAcademicSessions");
            query.setCacheable(true);
            return query.list();
        }
    });
}

From source file:org.sakaiproject.tool.assessment.facade.QuestionPoolFacadeQueries.java

License:Educational Community License

public List getSubPoolSizes(final String agent) {
    final HibernateCallback hcb = new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query q = session.createQuery(
                    "select a.questionPoolId, (select count(*) from QuestionPoolData b where b.parentPoolId=a.questionPoolId) "
                            + "from QuestionPoolData a where a.ownerId=?");
            q.setCacheable(true);
            q.setString(0, agent);/*w w w .ja v a  2  s. c  om*/
            return q.list();
        };
    };
    return getHibernateTemplate().executeFind(hcb);
}

From source file:org.sakaiproject.tool.assessment.facade.QuestionPoolFacadeQueries.java

License:Educational Community License

public int getSubPoolSize(final Long poolId) {
    final HibernateCallback hcb = new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query q = session
                    .createQuery("select count(qpp) from QuestionPoolData qpp where qpp.parentPoolId=?");
            q.setCacheable(true);
            q.setLong(0, poolId.longValue());
            return q.uniqueResult();
        };/*  ww  w .jav a  2s.  c o  m*/
    };

    Integer count = (Integer) getHibernateTemplate().execute(hcb);
    return count.intValue();
}