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.hyperic.hq.measurement.server.session.MeasurementDAO.java

License:Open Source License

/**
 * Look up a Measurement, allowing for the query to return a stale copy (for
 * efficiency reasons).//from  w  w  w .  jav  a 2  s  . c o m
 * 
 * @param tid The MeasurementTemplate id
 * @param iid The instance id
 * @param allowStale <code>true</code> to allow stale copies of an alert
 *        definition in the query results; <code>false</code> to never allow
 *        stale copies, potentially always forcing a sync with the database.
 * @return The Measurement or <code>null</code>.
 */
Measurement findByTemplateForInstance(Integer tid, Integer iid, boolean allowStale) {
    Session session = getSession();
    FlushMode oldFlushMode = session.getFlushMode();

    try {
        if (allowStale) {
            session.setFlushMode(FlushMode.MANUAL);
        }

        String sql = "select distinct m from Measurement m " + "join m.template t "
                + "where t.id=? and m.instanceId=?";

        return (Measurement) getSession().createQuery(sql).setInteger(0, tid.intValue())
                .setInteger(1, iid.intValue()).setCacheable(true)
                .setCacheRegion("Measurement.findByTemplateForInstance").uniqueResult();
    } finally {
        session.setFlushMode(oldFlushMode);
    }
}

From source file:org.jboss.as.test.compat.jpa.hibernate.transformer.SFSBHibernateSessionFactory.java

License:Open Source License

public FlushMode getFlushModeFromSessionTest(FlushMode flushMode) {
    Session session = sessionFactory.openSession();
    Transaction transaction = session.beginTransaction();
    try {//from  ww  w . j  a va2s.  co m
        if (flushMode != null) {
            session.setFlushMode(flushMode);
        }
        return session.getFlushMode();
    } finally {
        transaction.rollback();
        session.close();
    }
}

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

License:Apache License

/**
 * Get all registered datasource entries
 *//* w  w  w  . j a va2s  . c  o m*/
public List<DataSourceEntry> getDataSourceEntries() {
    final List result = new ArrayList();
    try {
        new HibernateTxFragment() {
            protected void txFragment(Session session) throws Exception {
                Query query = session.createQuery(" from " + DataSourceEntry.class.getName());
                FlushMode oldFlushMode = session.getFlushMode();
                session.setFlushMode(FlushMode.COMMIT);
                query.setCacheable(true);
                result.addAll(query.list());
                session.setFlushMode(oldFlushMode);
            }
        }.execute();
    } catch (Exception e) {
        log.error("Error: ", e);
    }
    return result;
}

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);/*w  w  w.  j a v a  2 s  .  com*/
            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  w  ww.ja  va  2s.  c o m*/
            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);/*  w w  w . j a va2s .  c  om*/
            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);//from  ww  w .j ava  2  s.  c om
            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  w w w  .  j av a  2 s .c o  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 av  a2s.com*/
            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 va  2s .  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 code=" + code + " does not exist.");
    return null;
}