Example usage for org.hibernate FlushMode COMMIT

List of usage examples for org.hibernate FlushMode COMMIT

Introduction

In this page you can find the example usage for org.hibernate FlushMode COMMIT.

Prototype

FlushMode COMMIT

To view the source code for org.hibernate FlushMode COMMIT.

Click Source Link

Document

The Session is flushed when Transaction#commit is called.

Usage

From source file:org.castafiore.persistence.LoadHibernateSessionFilter.java

License:Open Source License

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    SessionFactory factory = SpringUtil.getBeanOfType(SessionFactory.class);
    Map castaSession = SpringUtil.getBean("casta_session");
    Session s = null;//from  w  w w .ja  va  2  s . com
    if (!castaSession.containsKey("hibernate.session")) {
        s = factory.openSession();
        s.setFlushMode(FlushMode.COMMIT);
        s.setCacheMode(CacheMode.IGNORE);
        castaSession.put("hibernate.session", s);

    } else {
        s = (Session) castaSession.get("hibernate.session");
    }

    //t = null;
    try {
        if (t == null) {
            t = s.beginTransaction();
            creatorHash = request.hashCode();
        }

        if (!t.isActive()) {
            t.begin();
            creatorHash = request.hashCode();
        }

        chain.doFilter(request, response);

        //s.flush();
        if (t.isActive()) {
            if (creatorHash == request.hashCode()) {
                s.flush();
                t.commit();
                s.clear();

            }
        }

    } catch (Exception e) {
        e.printStackTrace();
        if (t != null && t.isActive()) {
            if (creatorHash == request.hashCode()) {
                t.rollback();
                s.clear();
            }
        }

        if (s != null) {

        }
    }

}

From source file:org.celllife.idart.database.hibernate.util.HibernateUtil.java

License:Open Source License

public Session getSession() {
    Session sess = sessionFactory.openSession();
    sess.setFlushMode(FlushMode.COMMIT);
    return sess;
}

From source file:org.cgiar.ccafs.ap.data.dao.mysqlhiberate.StandardDAO.java

License:Open Source License

/**
 * This method opens a session to the database.
 * //from   www.  j  ava2s  .co m
 * @return a Session object.
 */
private Session openSession() {
    Session session = null;
    session = sessionFactory.openSession();
    // Calling flush when committing change.
    session.setFlushMode(FlushMode.COMMIT);
    return session;

}

From source file:org.cgiar.ccafs.marlo.data.dao.mysql.AbstractMarloDAO.java

License:Open Source License

/**
 * This method make a query that returns a not mapped object result from the model.
 * //  ww w  . j a  va  2  s .c o m
 * @param sqlQuery is a string representing an SQL query.
 */
public List<Map<String, Object>> excuteStoreProcedure(String storeProcedure, String sqlQuery) {
    this.sessionFactory.getCurrentSession().createSQLQuery(storeProcedure).executeUpdate();
    Query query = this.sessionFactory.getCurrentSession().createSQLQuery(sqlQuery);
    query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
    query.setFlushMode(FlushMode.COMMIT);

    List<Map<String, Object>> result = query.list();
    return result;

}

From source file:org.cgiar.ccafs.marlo.data.dao.mysql.AbstractMarloDAO.java

License:Open Source License

/**
 * Pass String based hibernate query.//www  .java  2 s  . com
 * 
 * @param sqlQuery
 */
public void executeUpdateQuery(String sqlQuery) {

    Query query = this.sessionFactory.getCurrentSession().createSQLQuery(sqlQuery);
    query.setFlushMode(FlushMode.COMMIT);
    query.executeUpdate();
}

From source file:org.cgiar.ccafs.marlo.data.dao.mysql.AbstractMarloDAO.java

License:Open Source License

protected List<T> findAll(Query hibernateQuery) {
    hibernateQuery.setFlushMode(FlushMode.COMMIT);
    @SuppressWarnings("unchecked")
    List<T> list = hibernateQuery.list();
    return list;/*from w w w.  j  av  a  2  s . c  om*/
}

From source file:org.cgiar.ccafs.marlo.data.dao.mysql.AbstractMarloDAO.java

License:Open Source License

/**
 * This method make a query that returns a list of objects from the model.
 * This method was implemented in a generic way, so, the list of objects to be returned will depend on how the method
 * is being called./*from   w  ww  . j  a  v a  2  s  . co m*/
 * e.g:
 * List<SomeObject> list = this.findAll("some hibernate query");
 * or
 * this.<SomeObject>findAll("some hibernate query");
 * 
 * @param hibernateQuery is a string representing an HQL query.
 * @return a list of <T> objects.
 */
protected List<T> findAll(String hibernateQuery) {
    Query query = sessionFactory.getCurrentSession().createQuery(hibernateQuery);
    query.setFlushMode(FlushMode.COMMIT);
    return this.findAll(query);
}

From source file:org.cgiar.ccafs.marlo.data.dao.mysql.AbstractMarloDAO.java

License:Open Source License

/**
 * This method make a query that returns a not mapped object result from the model.
 * //from  w  w  w . j av  a2  s . c om
 * @param sqlQuery is a string representing an HQL query.
 */
public List<Map<String, Object>> findCustomQuery(String sqlQuery) {
    Query query = sessionFactory.getCurrentSession().createSQLQuery(sqlQuery);
    query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
    query.setFlushMode(FlushMode.COMMIT);
    List<Map<String, Object>> result = query.list();

    return result;

}

From source file:org.cgiar.ccafs.marlo.data.dao.mysql.AbstractMarloDAO.java

License:Open Source License

protected List<T> findEveryone(Class<T> clazz) {
    Query query = sessionFactory.getCurrentSession().createQuery("from " + clazz.getName());
    query.setFlushMode(FlushMode.COMMIT);

    @SuppressWarnings("unchecked")
    List<T> list = query.list();
    return list;/*from  w ww .j  a  va2s  .c o m*/

}

From source file:org.cgiar.ccafs.marlo.data.dao.mysql.AbstractMarloDAO.java

License:Open Source License

/**
 * Allows clients to create the HibernateQuery and set parameters on it.
 * /*from  www  .j  av  a2s .  c o  m*/
 * @param clazz
 * @param hibernateQuery
 * @return
 */
protected T findSingleResult(Class<T> clazz, Query hibernateQuery) {
    hibernateQuery.setFlushMode(FlushMode.COMMIT);
    T object = clazz.cast(hibernateQuery.uniqueResult());
    return object;
}