Search code examples
javaspringcachingspring-cachespelevaluationexception

How to use an objects' property as the key for @CachePut?


public interface MyRespository extends CrudRepository<MyEntity, Long> {
    @CachePut(value = "mycache", key = "id")
    @Override
    public <S extends MyEntity> S save(S entity);
}

@Entity
public class MyEntity {
    @Id
    private Long id;

    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
}

Result:

org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'id' cannot be found on object of type 'org.springframework.cache.interceptor.CacheExpressionRootObject' - maybe not public?


Solution

  • Accessing the object by index with key = "#a0.id" worked, but I still don't know why it's not possible to access the object by its name.