Search in sources :

Example 1 with CacheMode

use of org.hibernate.CacheMode in project hibernate-orm by hibernate.

the class DefaultDeleteEventListener method cascadeBeforeDelete.

protected void cascadeBeforeDelete(EventSource session, EntityPersister persister, Object entity, EntityEntry entityEntry, Set transientEntities) throws HibernateException {
    CacheMode cacheMode = session.getCacheMode();
    session.setCacheMode(CacheMode.GET);
    session.getPersistenceContext().incrementCascadeLevel();
    try {
        // cascade-delete to collections BEFORE the collection owner is deleted
        Cascade.cascade(CascadingActions.DELETE, CascadePoint.AFTER_INSERT_BEFORE_DELETE, session, persister, entity, transientEntities);
    } finally {
        session.getPersistenceContext().decrementCascadeLevel();
        session.setCacheMode(cacheMode);
    }
}
Also used : CacheMode(org.hibernate.CacheMode)

Example 2 with CacheMode

use of org.hibernate.CacheMode in project hibernate-orm by hibernate.

the class AbstractProducedQuery method beforeQuery.

protected void beforeQuery() {
    if (optionalId == null) {
        getQueryParameterBindings().verifyParametersBound(isCallable());
    }
    assert sessionFlushMode == null;
    assert sessionCacheMode == null;
    if (flushMode != null) {
        sessionFlushMode = getProducer().getHibernateFlushMode();
        getProducer().setHibernateFlushMode(flushMode);
    }
    final CacheMode effectiveCacheMode = CacheModeHelper.effectiveCacheMode(cacheStoreMode, cacheRetrieveMode);
    if (effectiveCacheMode != null) {
        sessionCacheMode = getProducer().getCacheMode();
        getProducer().setCacheMode(effectiveCacheMode);
    }
}
Also used : CacheMode(org.hibernate.CacheMode)

Example 3 with CacheMode

use of org.hibernate.CacheMode in project openmrs-core by openmrs.

the class HibernateContextDAO method updateSearchIndexForType.

@Override
@Transactional
public void updateSearchIndexForType(Class<?> type) {
    // From http://docs.jboss.org/hibernate/search/3.3/reference/en-US/html/manual-index-changes.html#search-batchindex-flushtoindexes
    FullTextSession session = Search.getFullTextSession(sessionFactory.getCurrentSession());
    session.purgeAll(type);
    // Prepare session for batch work
    session.flush();
    session.clear();
    FlushMode flushMode = session.getFlushMode();
    CacheMode cacheMode = session.getCacheMode();
    try {
        session.setFlushMode(FlushMode.MANUAL);
        session.setCacheMode(CacheMode.IGNORE);
        // Scrollable results will avoid loading too many objects in memory
        ScrollableResults results = session.createCriteria(type).setFetchSize(1000).scroll(ScrollMode.FORWARD_ONLY);
        int index = 0;
        while (results.next()) {
            index++;
            // index each element
            session.index(results.get(0));
            if (index % 1000 == 0) {
                // apply changes to indexes
                session.flushToIndexes();
                // free memory since the queue is processed
                session.clear();
            }
        }
        session.flushToIndexes();
        session.clear();
    } finally {
        session.setFlushMode(flushMode);
        session.setCacheMode(cacheMode);
    }
}
Also used : FullTextSession(org.hibernate.search.FullTextSession) CacheMode(org.hibernate.CacheMode) ScrollableResults(org.hibernate.ScrollableResults) FlushMode(org.hibernate.FlushMode) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with CacheMode

use of org.hibernate.CacheMode in project hibernate-orm by hibernate.

the class SessionImpl method refresh.

@Override
public void refresh(Object entity, LockModeType lockModeType, Map<String, Object> properties) {
    checkOpen();
    final CacheMode previousCacheMode = getCacheMode();
    final CacheMode refreshCacheMode = determineAppropriateLocalCacheMode(properties);
    LockOptions lockOptions = null;
    try {
        setCacheMode(refreshCacheMode);
        if (!contains(entity)) {
            throw exceptionConverter.convert(new IllegalArgumentException("Entity not managed"));
        }
        if (lockModeType != null) {
            if (!LockModeType.NONE.equals(lockModeType)) {
                checkTransactionNeeded();
            }
            lockOptions = buildLockOptions(lockModeType, properties);
            refresh(entity, lockOptions);
        } else {
            refresh(entity);
        }
    } catch (MappingException e) {
        throw exceptionConverter.convert(new IllegalArgumentException(e.getMessage(), e));
    } catch (RuntimeException e) {
        throw exceptionConverter.convert(e, lockOptions);
    } finally {
        setCacheMode(previousCacheMode);
    }
}
Also used : LockOptions(org.hibernate.LockOptions) CacheMode(org.hibernate.CacheMode) UnknownSqlResultSetMappingException(org.hibernate.procedure.UnknownSqlResultSetMappingException) MappingException(org.hibernate.MappingException)

Example 5 with CacheMode

use of org.hibernate.CacheMode in project hibernate-orm by hibernate.

the class DefaultDeleteEventListener method cascadeAfterDelete.

protected void cascadeAfterDelete(EventSource session, EntityPersister persister, Object entity, Set transientEntities) throws HibernateException {
    CacheMode cacheMode = session.getCacheMode();
    session.setCacheMode(CacheMode.GET);
    session.getPersistenceContext().incrementCascadeLevel();
    try {
        // cascade-delete to many-to-one AFTER the parent was deleted
        Cascade.cascade(CascadingActions.DELETE, CascadePoint.BEFORE_INSERT_AFTER_DELETE, session, persister, entity, transientEntities);
    } finally {
        session.getPersistenceContext().decrementCascadeLevel();
        session.setCacheMode(cacheMode);
    }
}
Also used : CacheMode(org.hibernate.CacheMode)

Aggregations

CacheMode (org.hibernate.CacheMode)5 FlushMode (org.hibernate.FlushMode)1 LockOptions (org.hibernate.LockOptions)1 MappingException (org.hibernate.MappingException)1 ScrollableResults (org.hibernate.ScrollableResults)1 UnknownSqlResultSetMappingException (org.hibernate.procedure.UnknownSqlResultSetMappingException)1 FullTextSession (org.hibernate.search.FullTextSession)1 Transactional (org.springframework.transaction.annotation.Transactional)1