Level 2 Sleep Manipulation

I use hibernate as my ORM solution, and EHCache as a second level cache (read-write).

My question is: is it possible to directly access the second level cache?

I want to access it: http://www.hibernate.org/hib_docs/v3/api/org/hibernate/cache/ReadWriteCache.html

How can I access the same ReadWriteCache that is used by Hibernate?

I have some direct / custom JDBC inserts that I do and I want to add these objects to the second level cache.

+5
source share
4 answers

"afterInsert" EntityPersister, , "/" concurrency. , Hibernate 3.3. 100%, , .

EntityPersister persister = ((SessionFactoryImpl) session.getSessionFactory()).getEntityPersister("theNameOfYourEntity");

if (persister.hasCache() && 
    !persister.isCacheInvalidationRequired() && 
    session.getCacheMode().isPutEnabled()) {

    CacheKey ck = new CacheKey( 
                    theEntityToBeCached.getId(), 
                    persister.getIdentifierType(), 
                    persister.getRootEntityName(), 
                    session.getEntityMode(), 
                    session.getFactory() 
                );

    persister.getCacheAccessStrategy().afterInsert(ck, theEntityToBeCached, null);
}

-

/**
 * Called after an item has been inserted (after the transaction completes),
 * instead of calling release().
 * This method is used by "asynchronous" concurrency strategies.
 *
 * @param key The item key
 * @param value The item
 * @param version The item version value
 * @return Were the contents of the cache actual changed by this operation?
 * @throws CacheException Propogated from underlying {@link org.hibernate.cache.Region}
 */
public boolean afterInsert(Object key, Object value, Object version) throws CacheException;
+6

, . EhCacheProvider , . CacheManager, manager.getCache(class_name), . CacheKey , :

  CacheKey cacheKey = new CacheKey(key, type, class_name, EntityMode.POJO,
    (SessionFactoryImplementor)session.getSessionFactory());

- , , , , .

, CacheProvider, SessionFactory, .

+1

Both sleep mode and JPA now provide direct access to the second level basic cache:

sessionFactory.getCache();
entityManager.getCache();
0
source

All Articles