Example usage for org.hibernate Session getFlushMode

List of usage examples for org.hibernate Session getFlushMode

Introduction

In this page you can find the example usage for org.hibernate Session getFlushMode.

Prototype

@Override
FlushModeType getFlushMode();

Source Link

Document

For users of the Hibernate native APIs, we've had to rename this method as defined by Hibernate historically because the JPA contract defines a method of the same name, but returning the JPA FlushModeType rather than Hibernate's FlushMode .

Usage

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);
            }/*from w ww.j a  v a2 s  . 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);/*w  ww. j  a v a  2s . c om*/
            results.addAll(query.list());
            session.setFlushMode(flushMode);
        }
    }.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.
 *//*ww w.  j av a  2  s  .  c  om*/
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);//from   w w w.  j  a  va 2  s . c o m
            results.addAll(query.list());
            session.setFlushMode(flushMode);
        }
    }.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  w w  w  . j a v a  2s  . c o  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 {/* w  w  w  .  j av  a 2s.com*/
        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;
}

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

License:Apache License

/**
 * Returns a given section according to its identifier
 *//*from   ww  w .j av a 2 s .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 {//from w  ww.java 2 s  .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
 *///w ww  .  j a v  a 2 s . c o m
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/*from  w  ww  .jav a2s .  co m*/
 */
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()]);
}