3
votes

Using Spring+JPA+Hibernate+Ehcache.

I am trying to cache Hibernate Collections in L2 Cache(Using EhCache).

The entity is annotated with @Cache(region=abc,strategy=READ_WRITE)

When I try to annotate the LAZY/EAGER loaded collection with @Cache annotation with same configurations, it results into the below mentioned exception:

java.lang.ClassCastException: org.hibernate.cache.ehcache.internal.nonstop.NonstopAwareEntityRegionAccessStrategy cannot be cast to org.hibernate.cache.spi.access.CollectionRegionAccessStrategy

The agenda here is to cache, all the data in the database on application start, to get the best performance.

2

2 Answers

1
votes

The work-around to this apparent bug is to name your collection and entity regions differently:

@Entity
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY, region="foo")
public class Foo {
    @Cache(usage=CacheConcurrencyStrategy.READ_WRITE, region="bar-list")
    @OneToMany
    private List<Bar> bars = new ArrayList<>();
}
0
votes

This is fixed in Hibernate version - 5.2.17.Final

But keep in mind, you might want to use NONSTRICT_READ_WRITE if you are hoping for the cached object to be removed from a collection upon a jpa delete.

See NonStrictReadWriteEhcacheCollectionRegionAccessStrategy vs ReadWriteEhcacheCollectionRegionAccessStrategy as explained here http://forums.terracotta.org/forums/posts/list/4413.page