Adriano Castro
Adriano Castro

Reputation: 1471

ClassCastException with EhCache 2.6.0

I'm using EhCache to store a limited size cache of my application objects but after upgrading its version I'm getting this exception when using cache.get(...):

java.lang.ClassCastException: net.sf.ehcache.store.chm.SelectableConcurrentHashMap$DummyPinnedKey cannot be cast to br.com.project.util.VttcElement

Does anybody know if is there any new interface we should implement in our cached keys?

Here's my adapted code:

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;

private VttcElementCache() {
    CacheManager cacheManager = CacheManager.create(METAINFFileLoader.getInputStream("ehcache.xml"));
    cache = cacheManager.getCache("br.com.project.util.VttcElement");
}

private void load(int id, long idf) {
    cache.put(new Element(new VttcElement(id, idf), "1"));
}

private String get(VttcElement vttcElement) {
    Element element = cache.get(vttcElement); //The exception is thrown here!
    (String) return elemente.getValue();
}

Upvotes: 2

Views: 2159

Answers (1)

Adriano Castro
Adriano Castro

Reputation: 1471

I found it.

For some reason my key class VttcElement was not been well stored inside the cache because the exception was thrown inside its equals method right in the line it makes (VttcElement) obj.

I changed all my keys to Strings instead VttcElement using its attributes concatenated with comma and it worked as key perfectly.

Thanks a lot.

Upvotes: 1

Related Questions