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.xchain.namespaces.hibernate.DebugSessionCommand.java

License:Apache License

private void logSession(Session session) {
    log.debug("Connected, Dirty, Open:  {}",
            new boolean[] { session.isConnected(), session.isDirty(), session.isOpen() });
    log.debug("CacheMode:  {}", session.getCacheMode().toString());
    log.debug("EntityMode: {}", session.getEntityMode().toString());
    log.debug("FlushMode:  {}", session.getFlushMode().toString());
}

From source file:org.zanata.model.validator.UniqueValidator.java

License:Open Source License

private int countRows(Object value) {
    // we need to use entityManager.unwrap because  injected session will
    // be a weld proxy and criteria.getExecutableCriteria method will try
    // to cast it to SessionImplementor (ClassCastException)
    Session session = entityManager.unwrap(Session.class);
    ClassMetadata metadata = session.getSessionFactory().getClassMetadata(value.getClass());
    String idName = metadata.getIdentifierPropertyName();
    // FIXME was EntityMode.POJO
    Serializable id = metadata.getIdentifier(value, null);

    DetachedCriteria criteria = DetachedCriteria.forClass(value.getClass());
    for (String property : parameters.properties()) {
        // FIXME was EntityMode.POJO
        criteria.add(Restrictions.eq(property, metadata.getPropertyValue(value, property)));
    }/*from w  ww . j a va 2 s.com*/

    // Id property
    if (id != null) {
        criteria.add(Restrictions.ne(idName, id));
    }
    criteria.setProjection(Projections.rowCount());

    // change the flush mode temporarily to perform the query or else
    // incomplete entities will try to get flushed
    // After the query, go back to the original mode
    FlushMode flushMode = session.getFlushMode();
    session.setFlushMode(FlushMode.MANUAL);
    List results = criteria.getExecutableCriteria(session).list();
    Number count = (Number) results.iterator().next();
    session.setFlushMode(flushMode);
    return count.intValue();
}

From source file:org.zkoss.zkgrails.ZKGrailsOpenSessionInViewListener.java

License:Open Source License

public void cleanup(Execution exec, Execution parent, List errs) {
    if (parent == null) { //the root execution of a servlet request
        try {// w ww  .  j  a  v a  2 s.  co m
            if (errs == null || errs.isEmpty()) {
                //if(sessionFactory.getCurrentSession().getTransaction().isActive()) {
                // Commit and cleanup
                log.debug("Committing the database transaction: " + exec);
                sessionFactory.getCurrentSession().getTransaction().commit();
                //}
            } else {
                final Throwable ex = (Throwable) errs.get(0);
                if (ex instanceof StaleObjectStateException) {
                    // default implementation does not do any optimistic concurrency
                    // control; it simply rollback the transaction.
                    handleStaleObjectStateException(exec, (StaleObjectStateException) ex);
                } else {
                    // default implementation log the stacktrace and then rollback
                    // the transaction.
                    handleOtherException(exec, ex);
                }
            }
        } finally {
            Session session = sessionFactory.getCurrentSession();
            if (!FlushMode.MANUAL.equals(session.getFlushMode())) {
                session.flush();
            }
            session.close();
        }
    }
}

From source file:won.protocol.util.hibernate.FlushModeSettingHibernateJpaDialect.java

License:Apache License

public Object prepareTransaction(EntityManager entityManager, boolean readOnly, String name)
        throws PersistenceException {

    Session session = getSession(entityManager);
    FlushMode currentFlushMode = session.getFlushMode();
    FlushMode previousFlushMode = null;//w  w  w .j  a va 2 s .c  om
    if (getFlushMode() != null) {
        session.setFlushMode(flushMode);
        previousFlushMode = currentFlushMode;
    } else if (readOnly) {
        // We should suppress flushing for a read-only transaction.
        session.setFlushMode(FlushMode.MANUAL);
        previousFlushMode = currentFlushMode;
    } else {
        // We need AUTO or COMMIT for a non-read-only transaction.
        if (currentFlushMode.lessThan(FlushMode.COMMIT)) {
            session.setFlushMode(FlushMode.AUTO);
            previousFlushMode = currentFlushMode;
        }
    }
    return new SessionTransactionData(session, previousFlushMode);
}