title: RedisTemplate hashvalue serializer to use for nested object with multiple types
body: <p>I'm trying to use Redis to store some cache data for my entity, which has different types of fields inside, for example,</p>
  
  <pre><code>public class Job {
      private String id;
      private Date createTime; //Long
      private String submitterName;
      private JobDefinition jobDef;  //Another class
  }
  </code></pre>
  
  <p>There are more fields and due to the fact that several fields are updated more frequently than others, I decided to save this <code>job</code> as a Hashmap in Redis with each field as a key. Here the nested object like <code>jobDef</code> is not important so I used <code>Jackson2JsonRedisSerializer</code> as <code>hashValueSerializer</code> for <code>RedisTemplate</code> and the <code>jobDef</code> obj will just be serialized as a long JSON string, which is totally fine in my case. </p>
  
  <p>But I don't know how can I effectively deserialize the whole <code>job</code> object back from Redis. The type I set to deserializer is like <code>Jackson2JsonRedisSerializer(Map.class)</code> but it complains when deserializing String keys and values. </p>
  
  <p>So is this an invalid usage with <code>RedisTemplate</code> or how should I configure my serializer for it?</p>
  
  <p>EDIT:
  Adding more code details,</p>
  
  <pre><code>    @Autowired
      private StringRedisTemplate redisTemplate; //Here I'm using a String template as I need to use the same redisTemplate for some key-value/list operations too
  
      Map jobHash= new ObjectMapper().convertValue(job, Map.class);
  
      redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer(Map.class));
  
      redisTemplate.opsForHash().putAll("job:"+job.getId(), jobHash); //After this the job hash shows up in Redis as I expected, while the jobDef member is serialized and saved as a JSON string
  
      Map jobMap = redisTemplate.opsForHash().entries("job:" + job.getId()); //But this won't work as it'll throw exception complaining cannot deserialize a String value to Map. But when I set Jackson2JsonRedisSerializer(String.class) it throws exception that cannot resolve the byte code
  </code></pre>
  
  <p>2nd EDIT:</p>
  
  <p>If using <code>JdkSerializationRedisSerializer</code> as HashValueSerializer in <code>RedisTemplate</code> then the deserialization works fine, however the downside for using this one is the value stored in Redis is not the same human readable string value as when using <code>Jackson2JsonRedisSerializer</code>. </p>