Example usage for org.hibernate Session setFlushMode

List of usage examples for org.hibernate Session setFlushMode

Introduction

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

Prototype

@Deprecated
void setFlushMode(FlushMode flushMode);

Source Link

Document

Set the flush mode for this session.

Usage

From source file:org.jboss.dashboard.database.DataSourceManager.java

License:Apache License

public DataSourceEntry getDataSourceEntry(final String name) throws Exception {
    if (name == null)
        return null;

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

            StringBuffer sql = new StringBuffer();
            sql.append("select dse ");
            sql.append("from ").append(DataSourceEntry.class.getName()).append(" as dse ");
            sql.append("where dse.name = :name");

            Query query = session.createQuery(sql.toString());
            query.setString("name", name);
            query.setCacheable(true);/*from  ww w  .  j  a v a 2 s .  co m*/
            results.addAll(query.list());
            session.setFlushMode(flushMode);
        }
    }.execute();
    if (results.size() > 0) {
        if (results.size() > 1)
            log.error("There are " + results.size() + " data sources with name=" + name);
        return (DataSourceEntry) results.get(0);
    } else {
        log.debug("Does not exists data source with name: " + name);
        return null;
    }
}

From source file:org.jboss.dashboard.kpi.KPIManagerImpl.java

License:Apache License

public Set<KPI> getAllKPIs() throws Exception {
    final Set<KPI> results = new HashSet<KPI>();
    new HibernateTxFragment() {
        protected void txFragment(Session session) throws Exception {
            FlushMode flushMode = session.getFlushMode();
            session.setFlushMode(FlushMode.COMMIT);
            Query query = session.createQuery("from " + KPIImpl.class.getName() + " order by id");
            query.setCacheable(true);/*from ww  w.  j  a  va 2  s  . com*/
            results.addAll(query.list());
            session.setFlushMode(flushMode);
        }
    }.execute();
    return results;
}

From source file:org.jboss.dashboard.kpi.KPIManagerImpl.java

License:Apache License

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

            StringBuffer sql = new StringBuffer();
            sql.append("from ").append(KPIImpl.class.getName()).append(" as instance ");
            sql.append("where instance.id = :id");

            Query query = session.createQuery(sql.toString());
            if (id != null)
                query.setLong("id", id.longValue());
            query.setCacheable(true);//from   ww w.j av  a2  s .  c o  m
            results.addAll(query.list());
            session.setFlushMode(flushMode);
        }
    }.execute();
    if (results.size() > 0)
        return (KPIImpl) results.get(0);
    else
        log.debug("KPI with id =" + id + " does not exist.");
    return null;
}

From source file:org.jboss.dashboard.kpi.KPIManagerImpl.java

License:Apache License

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

            StringBuffer sql = new StringBuffer();
            sql.append("from ").append(KPIImpl.class.getName()).append(" as instance ");
            sql.append("where instance.code = :code");

            Query query = session.createQuery(sql.toString());
            if (code != null)
                query.setString("code", code);
            query.setCacheable(true);/*  www  . j ava  2 s .  c o  m*/
            results.addAll(query.list());
            session.setFlushMode(flushMode);
        }
    }.execute();
    if (results.size() > 0)
        return (KPIImpl) results.get(0);
    else
        log.debug("KPI with code=" + code + " does not exist.");
    return null;
}

From source file:org.jboss.dashboard.provider.DataProviderManagerImpl.java

License:Apache License

public Set<DataProvider> getAllDataProviders() throws Exception {
    final Set<DataProvider> results = new HashSet<DataProvider>();
    new HibernateTxFragment() {
        protected void txFragment(Session session) throws Exception {
            FlushMode flushMode = session.getFlushMode();
            session.setFlushMode(FlushMode.COMMIT);
            Query query = session.createQuery("from " + DataProviderImpl.class.getName() + " order by id");
            query.setCacheable(true);//from  ww  w  .  j a  va 2s . co m
            results.addAll(query.list());
            session.setFlushMode(flushMode);
        }
    }.execute();
    return results;
}

From source file:org.jboss.dashboard.provider.DataProviderManagerImpl.java

License:Apache License

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

            StringBuffer sql = new StringBuffer();
            sql.append("from ").append(DataProviderImpl.class.getName()).append(" as instance ");
            sql.append("where instance.id = :id");

            Query query = session.createQuery(sql.toString());
            if (id != null)
                query.setLong("id", id.longValue());
            query.setCacheable(true);/*from w  ww  .  j a  va 2  s  .  c o  m*/
            results.addAll(query.list());
            session.setFlushMode(flushMode);
        }
    }.execute();
    if (results.size() > 0)
        return (DataProviderImpl) results.get(0);
    else
        log.debug("Data provider with id =" + id + " does not exist.");
    return null;
}

From source file:org.jboss.dashboard.provider.DataProviderManagerImpl.java

License:Apache License

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

            StringBuffer sql = new StringBuffer();
            sql.append("from ").append(DataProviderImpl.class.getName()).append(" as instance ");
            sql.append("where instance.code = :code");

            Query query = session.createQuery(sql.toString());
            if (code != null)
                query.setString("code", code);
            query.setCacheable(true);//w  w w  . j  a  v  a  2  s .  c  om
            results.addAll(query.list());
            session.setFlushMode(flushMode);
        }
    }.execute();
    if (results.size() > 0)
        return (DataProviderImpl) results.get(0);
    else
        log.debug("Data provider with code=" + code + " does not exist.");
    return null;
}

From source file:org.jboss.dashboard.security.PermissionManager.java

License:Apache License

/**
 * Find the permission descriptor for given principal and permission
 *///from www.  jav a2 s .  c  om
public PermissionDescriptor find(final Principal prpal, final Permission perm) {
    final List<PermissionDescriptor> results = new ArrayList<PermissionDescriptor>(1);
    HibernateTxFragment txFragment = new HibernateTxFragment() {
        protected void txFragment(Session session) throws Exception {
            StringBuffer buf = new StringBuffer();
            buf.append(
                    " from " + PermissionDescriptor.class.getName() + " as item where item.dbid is not null ");
            if (prpal != null) {
                buf.append(" and item.principalClass = :principalClass  ");
                buf.append(" and item.principalName =  :principalName  ");
            }
            buf.append(
                    "and item.permissionClass = :permissionClass and item.permissionResource = :permissionResource");
            Query query = session.createQuery(buf.toString());
            if (prpal != null) {
                query.setString("principalClass", prpal.getClass().getName());
                query.setString("principalName", prpal.getName());
            }
            query.setString("permissionClass", perm.getClass().getName());
            query.setString("permissionResource", perm.getName());
            query.setCacheable(true);
            FlushMode oldFlushMode = session.getFlushMode();
            session.setFlushMode(FlushMode.NEVER);
            results.addAll(query.list());
            session.setFlushMode(oldFlushMode);
        }
    };

    try {
        txFragment.execute();
        if (!results.isEmpty())
            return results.get(0);
        else
            return null;
    } catch (Exception e) {
        log.error("Error retrieving PermissionDescriptor", e);
        return null;
    }
}

From source file:org.jboss.dashboard.security.PermissionManager.java

License:Apache License

/**
 * Recover the Permissions for the given permission resource name
 *///from  w ww.  j a v  a 2 s .c  o m
public List<PermissionDescriptor> find(final String resourceName) throws Exception {
    final List<PermissionDescriptor> results = new ArrayList<PermissionDescriptor>();
    new HibernateTxFragment() {
        protected void txFragment(Session session) throws Exception {
            StringBuffer buf = new StringBuffer();
            buf.append(
                    " from " + PermissionDescriptor.class.getName() + " as item where item.dbid is not null ");
            if (!StringUtils.isBlank(resourceName))
                buf.append(" and item.permissionResource = :res1 or  item.permissionResource = :res2");
            Query query = session.createQuery(buf.toString());
            if (!StringUtils.isBlank(resourceName)) {
                query.setString("res1", resourceName);
                query.setString("res2", resourceName + ".*");
            }
            query.setCacheable(true);
            FlushMode oldFlushMode = session.getFlushMode();
            session.setFlushMode(FlushMode.NEVER);
            results.addAll(query.list());
            session.setFlushMode(oldFlushMode);
        }
    }.execute();
    return results;
}

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
 *///from   w ww  . ja v  a  2s .com
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;
}