0

When I execute following code

AppConfigurationCacheService appCache = getAppContext().getBean("appConfigurationCacheService");
appCache.getAppConfig("LANDING_PAGE");

I get a following exception:

java.lang.String cannot be cast to my.daomodel.AppConfiguration: java.lang.ClassCastException: java.lang.String cannot be cast to my.daomodel.AppConfiguration
    at my.service.cache.AppConfigurationCacheService$$EnhancerBySpringCGLI$$8a364268.getAppConfig(<generated>)
    

However, if I make the getAppConfig return String, then I no longer get the exception. So I'm not sure why getAppConfig method is trying to return String instead of AppConfiguration object. Did I configure the cache incorrectly? Please help.

Below are classes in detail
Thank you in advance.

Entity class

@Component
@Entity
@Table(name="APP_CONFIGURATION")
@SuppressWarnings("serial")
public class AppConfiguration implements Serializable{
    @Id
    @SequenceGenerator(name"AppConfigIdSeq" sequenceName="APP_CONFIG_ID_SEQ", initialValue=1, allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="AppConfigIdSeq")
    @Column(name="APP_CONFIG_ID", nullable=false)
    private long appConfigId;
    
    @Column(name="CONFIG_KEY", nullable=false)
    @Type(type="string")
    private String configKey;
    
    @Column(name="CONFIG_VALUE")
    @Type(type="string")
    private String configValue;
    
    public AppConfiguration(){
    }
    
    public long getAppConfigId(){
        return appConfigId;
    }
    
    public void setAppConfigId(long appConfigId){
        this.appConfigId = appConfigId;
    }
    
    public String getConfigKey(){
        return configKey;
    }
    
    public void setConfigKey(String configKey){
        this.configKey = configKey;
    }
    
    public String getConfigValue(){
        return configValue;
    }
    
    public void setConfigValue(String configValue) {
        this.configValue = configValue;
    }
}

Cache manager class

@Configuration
@EnableCaching
public class CaffeineCacheManager extends CachingConfigurerSupport{
    @Bean
    public CacheManager cacheManager(){
        CaffeineCacheManager cacheManager = new CaffeinieCacheManager("appConfigCache");
        cacheManager.setCaffeine(
            Caffeine.newBuilder()
                .initialCapacity(300)
                .maximumSize(1000)
                .recordStats()
            );
        return cacheManager;
    }
}

Cache service Class

@Service
@CacheConfig(cacheName={"appConfigCache"})
public class AppConfigurationCacheService{
    @Autowired
    private AppConfigurationDao appConfigurationDao;
    
    @Cacheable(key="#configKey")
    public AppConfiguration getAppConfig(String configKey){
        AppConfiguration appConfig = appConfigurationDao.getAppConfigurationByKey(configKey);
        return appConfig;
    }
}
T.J
  • 3
  • 3

3 Answers3

0

Sorry I'm not good at English. It seems that "getAppConfig" returns null.

  • I'm not sure why you say getAppConfig returns null when getAppConfigurationByKey method actually returns result from the database. – T.J Apr 18 '23 at 03:01
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 20 '23 at 08:53
0

It is possible that the key value is entered with a different value.

0

I had same issue, I guess you are using @Cacheable(key="#configKey") more than one place in different service code in your project. The duplicated configKey overwriting your current configKey information. That's why you are getting class cast exception.

Find where are all these key="#configKey" exactly you have used, rename with different key name. Problem solved.

kkrn
  • 1
  • 1