Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception while caching Hibernate Collection in L2 Cache Ehcache

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.

like image 476
Ankita Sahni Avatar asked Jan 20 '26 19:01

Ankita Sahni


1 Answers

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<>();
}
like image 128
Jan Nielsen Avatar answered Jan 23 '26 21:01

Jan Nielsen