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.cgiar.ccafs.marlo.data.dao.mysql.AbstractMarloDAO.java

License:Open Source License

/**
 * This method make a query that returns a single object result from the model.
 * This method was implemented in a generic way, so, the object to be returned will depend on how the method
 * is being called./*from   w  w  w  .ja  va2  s  .  co m*/
 * 
 * @param hibernateQuery is a string representing an HQL query.
 * @return a Object of <T>
 */
protected T findSingleResult(Class<T> clazz, String hibernateQuery) {
    Query query = sessionFactory.getCurrentSession().createQuery(hibernateQuery);
    query.setFlushMode(FlushMode.COMMIT);
    return this.findSingleResult(clazz, query);
}

From source file:org.cobbzilla.wizard.dao.AbstractCRUDDAO.java

public static void setFlushMode(HibernateTemplate template) {
    template.getSessionFactory().getCurrentSession().setFlushMode(FlushMode.COMMIT);
}

From source file:org.codehaus.grepo.query.hibernate.repository.DefaultHibernateRepository.java

License:Apache License

/**
 * Apply the flush mode that's been specified.
 *
 * @param sessionHolder The current session holder.
 * @param queryOptions the query options.
 *//*from w w w .j  av  a  2 s.  c o  m*/
protected void applyFlushMode(CurrentSessionHolder sessionHolder, HibernateQueryOptions queryOptions) {
    HibernateFlushMode flushModeToUse = getFlushMode(queryOptions);

    if (flushModeToUse != null) {
        FlushMode flushModeToSet = null;
        FlushMode previousFlushMode = null;

        if (flushModeToUse == HibernateFlushMode.MANUAL) {
            if (sessionHolder.isExistingTransaction()) {
                previousFlushMode = sessionHolder.getSession().getFlushMode();
                if (!previousFlushMode.lessThan(FlushMode.COMMIT)) {
                    flushModeToSet = FlushMode.MANUAL;
                }
            } else {
                flushModeToSet = FlushMode.MANUAL;
            }
        } else if (flushModeToUse == HibernateFlushMode.EAGER) {
            if (sessionHolder.isExistingTransaction()) {
                previousFlushMode = sessionHolder.getSession().getFlushMode();
                if (!previousFlushMode.equals(FlushMode.AUTO)) {
                    flushModeToSet = FlushMode.AUTO;
                }
            }
            // else rely on default FlushMode.AUTO
        } else if (flushModeToUse == HibernateFlushMode.COMMIT) {
            if (sessionHolder.isExistingTransaction()) {
                previousFlushMode = sessionHolder.getSession().getFlushMode();
                if (previousFlushMode.equals(FlushMode.AUTO) || previousFlushMode.equals(FlushMode.ALWAYS)) {
                    flushModeToSet = FlushMode.COMMIT;
                }
            } else {
                flushModeToSet = FlushMode.COMMIT;
            }
        } else if (flushModeToUse == HibernateFlushMode.ALWAYS) {
            if (sessionHolder.isExistingTransaction()) {
                previousFlushMode = sessionHolder.getSession().getFlushMode();
                if (!previousFlushMode.equals(FlushMode.ALWAYS)) {
                    flushModeToSet = FlushMode.ALWAYS;
                }
            } else {
                flushModeToSet = FlushMode.ALWAYS;
            }
        }

        if (flushModeToSet != null) {
            logger.debug("Setting flushMode to '{}' for generic repository execution", flushModeToSet);
            sessionHolder.getSession().setFlushMode(flushModeToSet);
        }

        if (previousFlushMode != null) {
            sessionHolder.setPreviousFlushMode(previousFlushMode);
        }
    }
}

From source file:org.eclipse.emf.teneo.hibernate.test.stores.HibernateTestStore.java

License:Open Source License

/** Sets the emfDataStore */
private void setDataStore() {
    // currentTimeMillis is added to make it unique
    if (ejb3) {/*from  w  ww.  j a  va2s  . c  o m*/
        emfDataStore = new HbEntityDataStore(); // (HbDataStore)HbHelper.INSTANCE.
        // createRegisterDataStore
        // (getDatabaseAdapter().getDbName());
    } else {
        emfDataStore = HbHelper.INSTANCE.createRegisterDataStore(getDatabaseAdapter().getDbName());
    }
    emfDataStore.setExtensionManager(extensionManager);
    emfDataStore.setName(getDatabaseAdapter().getDbName());
    if (props.getProperty(EPACKAGE_INIT_MODE) != null) {
        if (props.getProperty(EPACKAGE_INIT_MODE).compareTo(EPACKAGE_INIT_MODE_SPECIFIED) == 0) {
            final List<String> clss = Collections.singletonList(props.getProperty(SPECIFIED_EPACKAGES));
            emfDataStore.setEPackageClasses(clss);
        } else if (props.getProperty(EPACKAGE_INIT_MODE).compareTo(EPACKAGE_INIT_MODE_CLASS) == 0) {
            final List<String> clss = new ArrayList<String>();
            for (EPackage epackage : epackages) {
                clss.add(epackage.getClass().getName());
            }
            emfDataStore.setEPackageClasses(clss);
        } else if (props.getProperty(EPACKAGE_INIT_MODE).compareTo(EPACKAGE_INIT_MODE_ECORE) == 0) {
            final List<String> ecores = new ArrayList<String>();
            ecores.add(props.getProperty(EPACKAGE_INIT_MODE_ECORE_VALUE));
            emfDataStore.setEPackageFiles(ecores);
        }
    } else {
        emfDataStore.setEPackages(epackages);
    }
    // any value is fine
    if (props.getProperty(USE_DATASTORE_REGISTRY) != null) {
        PackageRegistryProvider.getInstance().setPackageRegistry(null);
        emfDataStore.setPackageRegistry(EPackage.Registry.INSTANCE);
    } else {
        PackageRegistryProvider.getInstance().setPackageRegistry(EPackage.Registry.INSTANCE);
    }

    // set both hibernate and persistence props as we do not know the
    // difference right now
    props.putAll(getHibernateProperties((HibernateTestDBAdapter) getDatabaseAdapter()));

    if (!props.containsKey(PersistenceOptions.JOIN_TABLE_FOR_NON_CONTAINED_ASSOCIATIONS)) {
        props.setProperty(PersistenceOptions.JOIN_TABLE_FOR_NON_CONTAINED_ASSOCIATIONS, "false");
    }

    // do a special trick for hsqldb, because hsqldb expects all identifiers
    // to be
    // escaped or otherwise uppercases them, so uppercase everything
    // automatically
    if (getDatabaseAdapter() instanceof HsqldbTestDatabaseAdapter
            && !props.containsKey(PersistenceOptions.SQL_CASE_STRATEGY)) {
        props.setProperty(PersistenceOptions.SQL_CASE_STRATEGY, "uppercase");
    }

    if (isEntityManagerStore()) {
        props.setProperty(PersistenceOptions.ALSO_MAP_AS_CLASS, "true");
    }

    emfDataStore.setDataStoreProperties(props);
    emfDataStore.initialize();
    if (sessionWrapper != null) {
        refresh();
    } else {
        sessionWrapper = emfDataStore.createSessionWrapper();
        sessionWrapper.getHibernateSession().setFlushMode(FlushMode.COMMIT);
    }
}

From source file:org.eclipse.emf.teneo.hibernate.test.stores.HibernateTestStore.java

License:Open Source License

/** Creates a new persistence manager, actually to clear the cache */
public void refresh() {

    if (sessionWrapper.isTransactionActive()) {
        sessionWrapper.rollbackTransaction();
    }// w  w w  .j  av  a  2s . co m
    sessionWrapper.close();
    sessionWrapper = emfDataStore.createSessionWrapper();
    sessionWrapper.getHibernateSession().setFlushMode(FlushMode.COMMIT);
}

From source file:org.grails.orm.hibernate.GrailsHibernateTemplate.java

License:Apache License

/**
 * Apply the flush mode that's been specified for this accessor to the given Session.
 *
 * @param session             the current Hibernate Session
 * @param existingTransaction if executing within an existing transaction
 * @return the previous flush mode to restore after the operation, or <code>null</code> if none
 * @see #setFlushMode/*from w  w  w .j  a  va2s.  c  o m*/
 * @see org.hibernate.Session#setFlushMode
 */
protected FlushMode applyFlushMode(Session session, boolean existingTransaction) {
    if (isApplyFlushModeOnlyToNonExistingTransactions() && existingTransaction) {
        return null;
    }

    if (getFlushMode() == FLUSH_NEVER) {
        if (existingTransaction) {
            FlushMode previousFlushMode = HibernateVersionSupport.getFlushMode(session);
            if (!previousFlushMode.lessThan(FlushMode.COMMIT)) {
                session.setFlushMode(FlushMode.MANUAL);
                return previousFlushMode;
            }
        } else {
            session.setFlushMode(FlushMode.MANUAL);
        }
    } else if (getFlushMode() == FLUSH_EAGER) {
        if (existingTransaction) {
            FlushMode previousFlushMode = HibernateVersionSupport.getFlushMode(session);
            if (!previousFlushMode.equals(FlushMode.AUTO)) {
                session.setFlushMode(FlushMode.AUTO);
                return previousFlushMode;
            }
        } else {
            // rely on default FlushMode.AUTO
        }
    } else if (getFlushMode() == FLUSH_COMMIT) {
        if (existingTransaction) {
            FlushMode previousFlushMode = HibernateVersionSupport.getFlushMode(session);
            if (previousFlushMode.equals(FlushMode.AUTO) || previousFlushMode.equals(FlushMode.ALWAYS)) {
                session.setFlushMode(FlushMode.COMMIT);
                return previousFlushMode;
            }
        } else {
            session.setFlushMode(FlushMode.COMMIT);
        }
    } else if (getFlushMode() == FLUSH_ALWAYS) {
        if (existingTransaction) {
            FlushMode previousFlushMode = HibernateVersionSupport.getFlushMode(session);
            if (!previousFlushMode.equals(FlushMode.ALWAYS)) {
                session.setFlushMode(FlushMode.ALWAYS);
                return previousFlushMode;
            }
        } else {
            session.setFlushMode(FlushMode.ALWAYS);
        }
    }
    return null;
}

From source file:org.grails.orm.hibernate.support.GrailsOpenSessionInViewInterceptor.java

License:Apache License

protected void applyFlushMode(Session session) {
    FlushMode hibernateFlushMode = FlushMode.AUTO;
    switch (getFlushMode()) {
    case GrailsHibernateTemplate.FLUSH_EAGER:
    case GrailsHibernateTemplate.FLUSH_AUTO:
        hibernateFlushMode = FlushMode.AUTO;
        break;/*  w  ww . j ava 2  s. c o  m*/
    case GrailsHibernateTemplate.FLUSH_NEVER:
        hibernateFlushMode = FlushMode.MANUAL;
        break;
    case GrailsHibernateTemplate.FLUSH_COMMIT:
        hibernateFlushMode = FlushMode.COMMIT;
        break;
    case GrailsHibernateTemplate.FLUSH_ALWAYS:
        hibernateFlushMode = FlushMode.ALWAYS;
        break;
    }
    session.setFlushMode(hibernateFlushMode);
}

From source file:org.grails.orm.hibernate4.support.GrailsOpenSessionInViewInterceptor.java

License:Apache License

protected void applyFlushMode(Session session) {
    FlushMode hibernateFlushMode = FlushMode.AUTO;
    switch (flushMode) {
    case AUTO:// w ww  .  j  a v a  2 s  .com
        hibernateFlushMode = FlushMode.AUTO;
        break;
    case MANUAL:
        hibernateFlushMode = FlushMode.MANUAL;
        break;
    case COMMIT:
        hibernateFlushMode = FlushMode.COMMIT;
        break;
    case ALWAYS:
        hibernateFlushMode = FlushMode.ALWAYS;
        break;
    }
    session.setFlushMode(hibernateFlushMode);
}

From source file:org.grails.orm.hibernate4.support.GrailsOpenSessionInViewInterceptor.java

License:Apache License

public void setFlushMode(int flushMode) {
    if (AbstractHibernateDatastore.FlushMode.AUTO.getLevel() == flushMode) {
        this.flushMode = AbstractHibernateDatastore.FlushMode.AUTO;
    } else if (AbstractHibernateDatastore.FlushMode.MANUAL.getLevel() == flushMode) {
        this.flushMode = AbstractHibernateDatastore.FlushMode.MANUAL;
    } else if (AbstractHibernateDatastore.FlushMode.COMMIT.getLevel() == flushMode) {
        this.flushMode = AbstractHibernateDatastore.FlushMode.COMMIT;
    } else if (AbstractHibernateDatastore.FlushMode.ALWAYS.getLevel() == flushMode) {
        this.flushMode = AbstractHibernateDatastore.FlushMode.ALWAYS;
    }/*w  ww  .  jav  a  2  s  .co m*/
}

From source file:org.hiberframe.HibernateSessionInterface.java

License:Open Source License

public Session getSession() {
    if (session == null) {
        // System.out.println("num open: " + (++no));
        session = HibUtils.getSession();
        session.setFlushMode(FlushMode.COMMIT);
    }/* w ww.  java2 s .c  om*/
    return session;
}