title: JCache: Incompatible cache key types specified, expected class java.lang.Object but class java.lang.String
body: <p>I am having an issue configuring a Cache in Spring boot. It was working fine and after some unrelated changes, it stopped working.</p>
  
  <p>I have the following cache configuration:</p>
  
  <pre><code>@Configuration
  public class UserMappingCacheConfig {
      public static final String USER_MAPPING_CACHE = "userMappingCache";
  
  @Bean(name = "userMappingCache")
  public Cache&lt;String, String&gt; getUserMappingCache(JCacheCacheManager cacheManager) {
      CacheManager cm = cacheManager.getCacheManager();
      Cache&lt;String, String&gt; cache = cm.getCache(USER_MAPPING_CACHE, String.class, String.class);
      if (cache == null)
          cache = cm.createCache(USER_MAPPING_CACHE, getUserMappingCacheConfiguration());
      return cache;
  }
  
  private MutableConfiguration&lt;String, String&gt; getUserMappingCacheConfiguration() {
      MutableConfiguration&lt;String, String&gt; configuration =
              new MutableConfiguration&lt;String, String&gt;()
                      .setStoreByValue(true)
                      .setExpiryPolicyFactory( FactoryBuilder.factoryOf(
                              new CreatedExpiryPolicy( Duration.ONE_DAY)
                      ));
      return configuration;
  }
  </code></pre>
  
  <p>And I use the cache in the following way:</p>
  
  <pre><code>@CacheResult(cacheName = "userMappingCache")
  public String getPayrollUserName(@CacheKey String userName, String description) {...}
  </code></pre>
  
  <p>However, I get the following exception when running the service:</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userMappingCache' defined in class path resource [UserMappingCacheConfig.class]: 
  Bean instantiation via factory method failed;
  nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.cache.Cache]: Factory method 'getUserMappingCache' threw exception; 
  nested exception is java.lang.ClassCastException: Incompatible cache key types specified, expected class java.lang.Object but class java.lang.String was specified
                  at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
      ....
      Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.cache.Cache]: Factory method 'getUserMappingCache' threw exception; 
  nested exception is java.lang.ClassCastException: Incompatible cache key types specified, expected class java.lang.Object but class java.lang.String was specified
                      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
                      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
      ....
      Caused by: java.lang.ClassCastException: Incompatible cache key types specified, expected class java.lang.Object but class java.lang.String was specified
                      at com.hazelcast.cache.impl.AbstractHazelcastCacheManager.getCache(AbstractHazelcastCacheManager.java:200) ~[hazelcast-3.10.jar:3.10]
                      at com.hazelcast.cache.impl.AbstractHazelcastCacheManager.getCache(AbstractHazelcastCacheManager.java:67) ~[hazelcast-3.10.jar:3.10]
  </code></pre>
  
  <p>I googled around, and found some entries which mostly relate similar issues to serialization of the keys/values. Is that the reason even with a primitive class? How can I fix it?
  Thanks in advance.</p>