0
@Autowired
private StringRedisTemplate stringRedisTemplate;

public void test(String key) {
    // IDEA prompts an error
    Map<String, String> entries1 = stringRedisTemplate.opsForHash().entries(key);
        
    // This is OK.
    HashOperations<String, String, String> opsForHash = stringRedisTemplate.opsForHash();
    Map<String, String> entries = opsForHash.entries(key);  
}
deHaar
  • 17,687
  • 10
  • 38
  • 51
Gray
  • 29
  • 3

1 Answers1

4

The problem Is that the method opsForHash() uses 2 generics, This is the signature:

public <HK, HV> HashOperations<K, HK, HV> opsForHash()

If you want to use a single line, you need to set the generics, just like:

Map<String, String> entries1 = stringRedisTemplate.<String, String>opsForHash().entries(key);

In your code, the second approach works because the compiler finds out the generics from the defined variable on the left side of the = operator.

Roberto
  • 8,586
  • 3
  • 42
  • 53