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.jboss.dashboard.workspace.WorkspaceImpl.java

License:Apache License

/**
 * Returns a given section according to its identifier
 *//* w  ww  .j  a  va 2s.  c om*/
public Section getSection(final Long id) {
    if (id == null)
        return null;
    final List<Section> candidates = new ArrayList<Section>();
    try {
        new HibernateTxFragment() {
            protected void txFragment(Session session) throws Exception {
                FlushMode oldFlushMode = session.getFlushMode();
                session.setFlushMode(FlushMode.NEVER);
                Query query = session.createQuery("from " + Section.class.getName() + " as section "
                        + "where section.workspace=:workspace and section.sectionId=:pageid");

                query.setParameter("workspace", WorkspaceImpl.this);
                query.setLong("pageid", id.longValue());
                query.setCacheable(true);
                candidates.addAll(query.list());
                session.setFlushMode(oldFlushMode);
            }
        }.execute();
    } catch (Exception e) {
        log.error("Error: ", e);
    }
    if (candidates.size() == 1) {
        return candidates.get(0);
    }
    return null;

}

From source file:org.jboss.dashboard.workspace.WorkspaceImpl.java

License:Apache License

public PanelInstance getPanelInstance(final Long id) {
    if (id == null)
        return null;
    final List<PanelInstance> candidates = new ArrayList<PanelInstance>();
    try {/*w w w.ja va 2s .c  o  m*/
        new HibernateTxFragment() {
            protected void txFragment(Session session) throws Exception {
                FlushMode oldFlushMode = session.getFlushMode();
                session.setFlushMode(FlushMode.NEVER);
                Query query = session.createQuery("from " + PanelInstance.class.getName() + " as instance "
                        + "where instance.workspace=:workspace and instance.instanceId=:instid");

                query.setParameter("workspace", WorkspaceImpl.this);
                query.setLong("instid", id.longValue());
                query.setCacheable(true);
                candidates.addAll(query.list());
                session.setFlushMode(oldFlushMode);
            }
        }.execute();
    } catch (Exception e) {
        log.error("Error: ", e);
    }
    if (candidates.size() == 1) {
        return candidates.get(0);
    }
    return null;
}

From source file:org.jboss.dashboard.workspace.WorkspaceImpl.java

License:Apache License

/**
 * Returns a given section according to its url
 *//*www .  j  ava2s. c  om*/
public Section getSectionByUrl(final String friendlyUrl) {
    if (friendlyUrl == null)
        return null;
    final List<Section> candidates = new ArrayList<Section>();
    try {
        new HibernateTxFragment() {
            protected void txFragment(Session session) throws Exception {
                FlushMode oldFlushMode = session.getFlushMode();
                session.setFlushMode(FlushMode.NEVER);
                Query query = session.createQuery("from " + Section.class.getName() + " as section "
                        + "where section.workspace=:workspace and section.friendlyUrl=:pageid");

                query.setParameter("workspace", WorkspaceImpl.this);
                query.setString("pageid", friendlyUrl);
                query.setCacheable(true);
                candidates.addAll(query.list());
                session.setFlushMode(oldFlushMode);
            }
        }.execute();
    } catch (Exception e) {
        log.error("Error: ", e);
    }
    if (candidates.size() == 1) {
        return candidates.get(0);
    }
    return null;
}

From source file:org.jboss.dashboard.workspace.WorkspacesManager.java

License:Apache License

/**
 * Return all workspaces/*  w w w .  j a va2  s  .  c  om*/
 */
public WorkspaceImpl[] getWorkspaces() {
    final List<WorkspaceImpl> workspaces = new ArrayList<WorkspaceImpl>();

    HibernateTxFragment txFragment = new HibernateTxFragment() {
        protected void txFragment(Session session) throws Exception {
            FlushMode oldFlushMode = session.getFlushMode();
            session.setFlushMode(FlushMode.NEVER);
            Query q = session.createQuery(" from " + WorkspaceImpl.class.getName());
            q.setCacheable(true);
            workspaces.addAll(q.list());
            session.setFlushMode(oldFlushMode);
        }
    };

    try {
        txFragment.execute();
    } catch (Exception e) {
        log.error("Error:", e);
    }
    return workspaces.toArray(new WorkspaceImpl[workspaces.size()]);
}

From source file:org.jboss.dashboard.workspace.WorkspacesManager.java

License:Apache License

public Workspace getWorkspaceByUrl(final String url) throws Exception {
    final Workspace[] workspace = new Workspace[1];
    new HibernateTxFragment() {
        protected void txFragment(Session session) throws Exception {
            FlushMode oldFlushMode = session.getFlushMode();
            session.setFlushMode(FlushMode.NEVER);
            Query q = session
                    .createQuery(" from " + WorkspaceImpl.class.getName() + " p where p.friendlyUrl = :url");
            q.setString("url", url);
            q.setCacheable(true);
            List l = q.list();/*from  w  w w.j  a  v a  2  s  . c  om*/
            if (l.size() == 1) {
                workspace[0] = (Workspace) l.get(0);
            }
            session.setFlushMode(oldFlushMode);
        }
    }.execute();
    return workspace[0];
}

From source file:org.jpos.gl.GLSession.java

License:Open Source License

private void addRules(Map<String, Object> ruleMap, Journal journal, List acctHierarchy, int offset)
        throws HibernateException, GLException {
    Query q = session.createQuery(
            "from org.jpos.gl.RuleInfo where journal=:journal and account in (:accts) order by id");
    q.setParameter("journal", journal);
    q.setParameterList("accts", acctHierarchy, new LongType());
    q.setCacheable(true);
    q.setCacheRegion("rules");
    Iterator iter = q.iterate();/*  www  .j  a v  a 2  s . c  o  m*/

    while (iter.hasNext()) {
        RuleInfo ri = (RuleInfo) iter.next();
        RuleEntry k = new RuleEntry(ri, ri.getAccount());
        RuleEntry re = (RuleEntry) ruleMap.get(k.getKey());
        if (re == null)
            ruleMap.put(k.getKey(), re = k);

        re.addOffset(offset);
    }
}

From source file:org.kuali.continuity.dao.hibernate.ReferenceDomainObjectDAOImpl.java

License:Educational Community License

@SuppressWarnings("unchecked")
public List<T> getReferenceDomainObjects() {
    String queryName = this.refType.getSimpleName();
    Query query = this.getSession().getNamedQuery(queryName + ".list");
    query.setCacheable(true);
    return query.list();
}

From source file:org.linagora.linshare.core.repository.hibernate.AllowedContactRepositoryImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<AllowedContact> searchContact(final String mail, final String firstName, final String lastName,
        final Guest guest) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(final Session session) throws HibernateException, SQLException {
            String mailQ = mail == null ? "" : mail;
            String firstNameQ = firstName == null ? "" : firstName;
            String lastNameQ = lastName == null ? "" : lastName;

            String queryString = "select ac from AllowedContact ac join ac.contact as contact where ac.owner= :guest and LOWER(contact.mail) like :mail and LOWER(contact.firstName) like :firstName and LOWER(contact.lastName) like :lastName";

            Query query = session.createQuery(queryString);
            query.setParameter("guest", guest);
            query.setParameter("mail", '%' + mailQ.toLowerCase() + '%');
            query.setParameter("firstName", '%' + firstNameQ.toLowerCase() + '%');
            query.setParameter("lastName", '%' + lastNameQ.toLowerCase() + '%');

            List<Query> queries = query.setCacheable(false).list();
            return queries;
        }/*  ww w.  j  a v a2  s. co m*/
    });
}

From source file:org.linagora.linshare.core.repository.hibernate.DocumentEntryRepositoryImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//w w w.j  ava 2s.co m
public List<DocumentEntry> retrieveUserDocumentEntriesWithMatchCriterion(
        SearchDocumentCriterion searchDocumentCriterion) {
    final QueryParameter queryParameter = buildQuery(searchDocumentCriterion, ANYWHERE);

    return getHibernateTemplate().executeFind(new HibernateCallback<List<DocumentEntry>>() {
        public List<DocumentEntry> doInHibernate(final Session session)
                throws HibernateException, SQLException {
            StringBuilder queryString = new StringBuilder(
                    "select docEntry from DocumentEntry docEntry join docEntry.entryOwner account join docEntry.document doc ");
            final Query query = session.createQuery(queryString.append(queryParameter.getQuery()).toString());
            // Put the objects in the query
            for (String key : queryParameter.getKey()) {
                query.setParameter(key, queryParameter.getParameter(key));
            }
            return query.setCacheable(true).list();
        }
    });
}

From source file:org.linagora.linshare.core.repository.hibernate.ShareEntryRepositoryImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override// w  w  w  .ja  v a2s. c  om
public List<ShareEntry> retrieveUserShareEntriesWithMatchCriterion(
        SearchDocumentCriterion searchDocumentCriterion) {

    final QueryParameter queryParameter = buildQuery(searchDocumentCriterion, ANYWHERE);

    return getHibernateTemplate().executeFind(new HibernateCallback<List<ShareEntry>>() {
        public List<ShareEntry> doInHibernate(final Session session) throws HibernateException, SQLException {

            StringBuilder queryString = new StringBuilder(
                    "select share from ShareEntry share join share.recipient recipient join share.documentEntry.document doc join share.entryOwner sender ");

            final Query query = session.createQuery(queryString.append(queryParameter.getQuery()).toString());

            // Put the objects in the query
            for (String key : queryParameter.getKey()) {
                query.setParameter(key, queryParameter.getParameter(key));
            }

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