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.security.PermissionManager.java

License:Apache License

/**
 * Recover Permissions for the given permission class and resource name, including or excluding the ones marked as readonly
 *//* w  w  w.  j a va  2  s . c o  m*/
public List<PermissionDescriptor> find(final String permissionClass, final String permissionResource,
        final Boolean includeReadOnly) {
    final List<PermissionDescriptor> results = new ArrayList<PermissionDescriptor>(10);
    HibernateTxFragment txFragment = new HibernateTxFragment() {
        protected void txFragment(Session session) throws Exception {
            StringBuffer buf = new StringBuffer(
                    " from " + PermissionDescriptor.class.getName() + " as item where item.dbid is not null ");
            buf.append(
                    "and item.permissionClass = :permissionClass and item.permissionResource = :permissionResource");
            if (!includeReadOnly)
                buf.append(" and item.readonly = :readonly");
            Query query = session.createQuery(buf.toString());
            query.setString("permissionClass", permissionClass);
            query.setString("permissionResource", permissionResource);
            if (!includeReadOnly)
                query.setBoolean("readonly", includeReadOnly);
            query.setCacheable(true);
            FlushMode oldFlushMode = session.getFlushMode();
            session.setFlushMode(FlushMode.NEVER);
            results.addAll(query.list());
            session.setFlushMode(oldFlushMode);
        }
    };
    try {
        txFragment.execute();
    } catch (Exception e) {
        log.error("Error retrieving PermissionDescriptors for permission class " + permissionClass
                + " and resource " + permissionResource, e);
    }
    return results;
}

From source file:org.jboss.dashboard.ui.panel.advancedHTML.HTMLDriver.java

License:Apache License

public HTMLText load(final PanelInstance instance) {
    final List<HTMLText> results = new ArrayList<HTMLText>();
    try {/*from   w ww .  j a  va2s .  co m*/
        new HibernateTxFragment() {
            protected void txFragment(Session session) throws Exception {
                FlushMode oldFlushMode = session.getFlushMode();
                session.setFlushMode(FlushMode.NEVER);
                Query query = session.createQuery(
                        " from " + HTMLText.class.getName() + " as text where text.panelInstance = :instance");
                query.setParameter("instance", instance);
                query.setCacheable(true);
                results.addAll(query.list());
                session.setFlushMode(oldFlushMode);
            }
        }.execute();
        HTMLText text = null;
        if (results.size() > 0)
            text = results.get(0);
        else
            log.debug("Does not exist a html_text for HTML panel");
        return text;
    } catch (Exception e) {
        log.error("Can't retrive a data for HTML panel ", e);
        return null;
    }
}

From source file:org.jboss.dashboard.ui.panel.dataSourceManagement.DataSourceTableManager.java

License:Apache License

public List<DataSourceTableEntry> getSelectedTablesEntries(final String datasource) throws Exception {
    final List<DataSourceTableEntry> existingEntries = new ArrayList<DataSourceTableEntry>();
    new HibernateTxFragment() {
        protected void txFragment(Session session) throws Exception {
            Query query = session.createQuery(" from " + DataSourceTableEntry.class.getName()
                    + " entry where entry.datasource = :datasource");
            query.setString("datasource", datasource);
            query.setCacheable(true);
            FlushMode oldFlushMode = session.getFlushMode();
            session.setFlushMode(FlushMode.COMMIT);
            existingEntries.addAll(query.list());
            session.setFlushMode(oldFlushMode);
        }// w  w  w . j  a v  a2s  .c  o  m
    }.execute();
    return existingEntries;
}

From source file:org.jboss.dashboard.ui.panel.dataSourceManagement.DataSourceTableManager.java

License:Apache License

public List<DataSourceColumnEntry> getSelectedColumnsEntries(final String datasource, final String tableName)
        throws Exception {
    final List<DataSourceColumnEntry> existingEntries = new ArrayList<DataSourceColumnEntry>();
    new HibernateTxFragment() {
        protected void txFragment(Session session) throws Exception {
            Query query = session.createQuery(" from " + DataSourceColumnEntry.class.getName()
                    + " entry where entry.datasource = :datasource and entry.tableName = :tableName");
            query.setString("datasource", datasource);
            query.setString("tableName", tableName);
            FlushMode oldFlushMode = session.getFlushMode();
            session.setFlushMode(FlushMode.NEVER);
            query.setCacheable(true);
            existingEntries.addAll(query.list());
            session.setFlushMode(oldFlushMode);
        }//from w w  w.ja v  a  2 s  .c o m
    }.execute();
    return existingEntries;
}

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

License:Apache License

public Panel getPanelByDbId(final Long panelId) throws Exception {
    if (panelId != null) {
        final List<Panel> results = new ArrayList<Panel>();
        new HibernateTxFragment() {
            protected void txFragment(Session session) throws Exception {
                FlushMode flushMode = session.getFlushMode();
                session.setFlushMode(FlushMode.COMMIT);

                StringBuffer sql = new StringBuffer();
                sql.append("select p ");
                sql.append("from ").append(Panel.class.getName()).append(" as p ");
                sql.append("where p.dbid = :dbid");

                Query query = session.createQuery(sql.toString());
                query.setLong("dbid", panelId);
                query.setCacheable(true);
                results.addAll(query.list());
                session.setFlushMode(flushMode);
            }/*  ww  w. jav  a 2s . c o  m*/
        }.execute();
        if (results.size() > 0)
            return results.get(0);
        else
            log.debug("Does not exists panel with DB id: " + panelId);
    }
    return null;
}

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

License:Apache License

public Panel getPanelById(final Long panelId) throws Exception {
    final List<Panel> results = new ArrayList<Panel>();
    new HibernateTxFragment() {
        protected void txFragment(Session session) throws Exception {
            FlushMode flushMode = session.getFlushMode();
            session.setFlushMode(FlushMode.COMMIT);

            StringBuffer sql = new StringBuffer();
            sql.append("select p ");
            sql.append("from ").append(Panel.class.getName()).append(" as p ");
            sql.append("where p.panelId = :panelId");

            Query query = session.createQuery(sql.toString());
            query.setLong("panelId", panelId);
            query.setCacheable(true);
            results.addAll(query.list());
            session.setFlushMode(flushMode);
        }/*ww  w .j  a v a 2s. co m*/
    }.execute();
    if (results.size() > 0)
        return results.get(0);
    else
        log.debug("Does not exists panel with id: " + panelId);
    return null;
}

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

License:Apache License

/**
 * Retrieves the panel instances with the given paramId as a panelParameter.
 *///from ww  w  .  j av a  2  s.co m
public Set<PanelInstance> getPanelsByParameter(final String paramId, final String value) throws Exception {
    final Set<PanelInstance> results = new HashSet<PanelInstance>();
    if (value == null || paramId == null)
        return results;

    new HibernateTxFragment() {
        protected void txFragment(Session session) throws Exception {
            FlushMode flushMode = session.getFlushMode();
            session.setFlushMode(FlushMode.COMMIT);

            StringBuffer sql = new StringBuffer();
            sql.append("select p ");
            sql.append("from ").append(PanelInstance.class.getName())
                    .append(" as p join p.panelParams as param ");
            sql.append("where param.idParameter = :paramId ");

            Query query = session.createQuery(sql.toString());
            query.setString("paramId", paramId);

            query.setCacheable(true);
            results.addAll(query.list());
            session.setFlushMode(flushMode);
        }
    }.execute();

    Set<PanelInstance> matchPanel = new HashSet<PanelInstance>();
    for (PanelInstance panelInstance : results) {
        Set<PanelParameter> panelParams = panelInstance.getPanelParams();
        for (PanelParameter panelParameter : panelParams) {
            if (paramId.equals(panelParameter.getIdParameter()) && value.equals(panelParameter.getValue())) {
                matchPanel.add(panelInstance);
            }
        }
    }
    return matchPanel;
}

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

License:Apache License

public Section getSectionByDbId(final Long dbid) throws Exception {
    if (dbid == null)
        return null;

    final List<Section> results = new ArrayList<Section>();
    new HibernateTxFragment() {
        protected void txFragment(Session session) throws Exception {
            FlushMode flushMode = session.getFlushMode();
            session.setFlushMode(FlushMode.COMMIT);

            StringBuffer sql = new StringBuffer();
            sql.append("select item ");
            sql.append("from ").append(Section.class.getName()).append(" as item ");
            sql.append("where item.dbid = :dbid");

            Query query = session.createQuery(sql.toString());
            query.setLong("dbid", dbid.longValue());
            query.setCacheable(true);
            results.addAll(query.list());
            session.setFlushMode(flushMode);
        }//from  www .  j av a2  s  .  c om
    }.execute();
    if (results.size() > 0)
        return results.get(0);
    else
        log.debug("Does not exists a section with dbid: " + dbid);
    return null;
}

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

License:Apache License

/**
 * Returns all the children for a given section
 *//*from   ww w. jav a2  s. co m*/
public Section[] getAllChildSections(final Long sectionId) {
    final List<Section> childSections = new ArrayList<Section>();

    try {
        new HibernateTxFragment() {
            protected void txFragment(Session session) throws Exception {
                FlushMode oldFlushMode = session.getFlushMode();
                session.setFlushMode(FlushMode.NEVER);

                StringBuffer hql = new StringBuffer("from ");
                hql.append(Section.class.getName()).append(" as section where section.workspace=:workspace");
                if (sectionId == null)
                    hql.append(" and section.parentSectionId is null");
                else
                    hql.append(" and section.parentSectionId=:parentSectionId");

                Query query = session.createQuery(hql.toString());
                query.setParameter("workspace", WorkspaceImpl.this);
                if (sectionId != null)
                    query.setLong("parentSectionId", sectionId);
                query.setCacheable(true);
                childSections.addAll(query.list());
                session.setFlushMode(oldFlushMode);
            }
        }.execute();
    } catch (Exception e) {
        log.error("Error: ", e);
    }
    Collections.sort(childSections);
    return childSections.toArray(new Section[childSections.size()]);
}

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

License:Apache License

public int getSectionsCount() {
    try {//from  w  w  w  . j av  a2s. c  om
        final int[] size = new int[1];
        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");

                query.setParameter("workspace", WorkspaceImpl.this);
                query.setCacheable(true);
                size[0] = query.list().size();
                session.setFlushMode(oldFlushMode);
            }
        }.execute();
        return size[0];
    } catch (Exception e) {
        log.error("Error: ", e);
    }
    return -1;
}