skip to Main Content

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
@EnableCaching
public 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

@Repository
public 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?

2

Answers


  1. Hibernate decorates the class, which is probably causing the problem, plus there are heavyweight references to the underlying database.

    I strongly recommend (whether you’re using Redis or not) defining a POJO version of your entity and converting the entity class into the POJO as early as possible, and converting the POJO to the entity as late as possible, so your application deals with POJOs, and receives, validates and emits (from its API) POJOs, and the entity is only used when interacting with the database.

    Store POJO instances in Redis instead of entity instances.

    Or to be more robust, serialise your POJO to json and store the json string in redis, deserialising it on the way back. Jackson (which come for free with spring boot, supports this out of the box. It’s super simple to implement, plus you can easily debug content in Redis by visual inspection of its content.

    Login or Signup to reply.
  2. I have found the solution of this problem. I was making mistake. The only answer I found was this Hibernate decorated entity is causing issue.
    It is not at all related to Hibernate.
    While we create redisTemplate we also adding key and value serialization.
    I was adding wrong seriazation.

        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer()) 
        redisTemplate.setHashKeySerializer(newJdkSerializationRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
    

    By Default seriazation is JDK and which is required. I was not using that and adding StringSeriazation so data was stored in String format in REDIS. So when we perform deserization we need to implement deserializer.

    For more details of Serialization refer below link
    https://medium.com/@betul5634/redis-serialization-with-spring-redis-data-lettuce-codec-1a1d2bc73d26

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search