I am trying to use Redis Cache to improve the performance of our application. I am referring this article for my implementation. I am getting this exception:
class com.entity.UserEntity cannot be cast to class com.entity.UserEntity
RedisConfig:
@Configuration@EnableCachingpublic class RedisConfig { @Value("${spring.redis.port}") int redisPort; @Value("${spring.redis.host}") String redisHost; @Bean public LettuceConnectionFactory redisLettuceConnectionFactory() { RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(redisHost,redisPort); return new LettuceConnectionFactory(redisStandaloneConfiguration); } @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisLettuceConnectionFactory()); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new JdkSerializationRedisSerializer()); redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.setEnableTransactionSupport(true); redisTemplate.afterPropertiesSet(); return redisTemplate; }}
User Entity:
@Entity@Getter@Setter@EqualsAndHashCode(callSuper=false)@Slf4j@Table(name="user")public class UserEntity extends BaseEntity implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "user_id") @Type(type = "uuid-char") private UUID userId;//Binary in DB and UUID here @OneToMany(fetch = FetchType.EAGER, mappedBy = "user", orphanRemoval = true, cascade= CascadeType.ALL) @JsonIgnore @ToStringExclude @Fetch(value = FetchMode.SUBSELECT) private List<EmailAddress> emailAddresses = new ArrayList<>(); . . .}
UserDaoImpl
@Repositorypublic class UserDaoImpl implements UserDao { private UserRepository userRepository; private HashOperations hashOperations; //to access Redis cache private static final String KEY = "User"; public UserDaoImpl(@Autowired RedisTemplate<String, Object> redisTemplate, @Autowired UserRepository userRepository) { this.userRepository = userRepository; hashOperations = redisTemplate.opsForHash(); } @Override public Optional<UserEntity> fetchUserById(String userId) throws Exception {// Getting Exception here (UserEntity) hashOperations.get(KEY,userId) UserEntity userEntity = (UserEntity) hashOperations.get(KEY,userId); Optional<UserEntity> userOptional = Optional.ofNullable(userEntity); if(!userOptional.isPresent()) { userOptional = userRepository.findByUserId(userId); if (userOptional.isPresent()) { System.out.println("Cache Miss User id:" + userOptional.get().getUserId()); hashOperations.put(KEY, userOptional.get().getUserId().toString(), userOptional.get()); } else { throw new Exception("User not present"); } } return userOptional; }}
I am successfully able to put UserEntity in the Cache, but then on retrieval I get the exception mentioned above. Most likely, the issue is because of Hibernate. When we cache the entity first time it puts Proxy/PersistentBag in place of Collections, so next time (in some other session) when we retrieve the Object back from Cache we are unable to cast it to the required entity. Can someone confirm that this is the issue, if yes, how to resolve it?