Bird
0
0

Identify the error in this cache key definition: @Cacheable(value = "items", key = "#item.id + #item.name")

medium📝 Debug Q14 of 15
Spring Boot - Caching
Identify the error in this cache key definition: @Cacheable(value = "items", key = "#item.id + #item.name")
ASpEL expressions cannot concatenate strings with + operator
BMissing parentheses around the expression
CThe key must be a constant string, not an expression
DUsing + operator without converting to string may cause wrong key
Step-by-Step Solution
Solution:
  1. Step 1: Analyze SpEL expression with + operator

    In SpEL, + can add numbers or concatenate strings. If item.id is numeric, adding to item.name (string) may cause type issues.
  2. Step 2: Understand type conversion in keys

    Without explicit string conversion, adding number + string may produce unexpected keys or errors.
  3. Final Answer:

    Using + operator without converting to string may cause wrong key -> Option D
  4. Quick Check:

    Type mismatch in key expression = error [OK]
Quick Trick: Convert all parts to string before concatenation in keys [OK]
Common Mistakes:
  • Assuming + always concatenates strings
  • Not converting numeric parts to string explicitly
  • Thinking key must be constant string only

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes