简体   繁体   中英

Unable to cast the Object to Entity Type

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM