SessionFactoryImpl doesn't cache and reuse existing collection cache region access strategies


SessionFactoryImpl caches and reuses entity cache regions & natural id cache regions in determineEntityRegionAccessStrategy and determineNaturalIdRegionAccessStrategy using the cacheAccessStrategiesMap, so you can use the same cache region (in my case an Infinispan 8.0.1 cache) for multiple entity types.

However cache region access strategies are not cached for collections. When iterating over metadata.getCollectionBindings() a CollectionRegion is built each time by calling regionFactory.buildCollectionRegion(...).

Interestingly, the created access strategy is added to the cacheAccessStrategiesMap but that map is not used to reuse an access strategy for a collection.

Creating an access strategy for the same Infinispan cache twice causes an exception when the PutFromLoadValidator is added to the cache again. Stacktrace below:

I can workaround this issue by using separate caches for each collection, however I believe this is a fault that could be alleviated by caching and reusing CollectionRegionAccessStrategy}}s in the same way that entities do, and given that the access strategies are added to {{cacheAccessStrategiesMap but not retrieved, I figure this is an oversight.

I have created a patch for this, and will add a pull request to this ticket. This corrected the issue for me except that Hibernate 5.0 appears to not support using the same cache region for multiple entities in read-write access mode... I'll ask about that another time.




Steve Ebersole
December 16, 2015, 6:11 PM

Applied Karl's change upstream

Gabriel Deroude
January 5, 2016, 4:01 PM

The following situation is broken though, with this fix:

  • using Hibernate 5.0.6 with ehcache;

  • using custom regions by defining them in ehcache.xml (<cache name = "myRegion" ... />)

  • annotating the entity classes with @Cache (... region="myRegion")

  • entity class contains collections

The result is "org.hibernate.cache.ehcache.internal.nonstop.NonstopAwareEntityRegionAccessStrategy cannot be cast to org.hibernate.cache.spi.access.CollectionRegionAccessStrategy", because the method determineCollectionRegionAccessStrategy in SessionFactoryImpl finds a region with the name required, but it is an entity region, not a collection region.

The same scenario works with 5.0.3.

Steve Ebersole
January 5, 2016, 5:21 PM

So the case you are wanting to report as now-broken is where you want to stuff both entity and collection data into a single named region? While "strange", I guess there is nothing inherently wrong with that. Regardless, that would be a new bug report. Please open a new JIRA linked to this one with a test case.


Steve Ebersole


Karl von Randow

Fix versions





Suitable for new contributors


Requires Release Note





Affects versions