如何使用对象的 属性 作为@CachePut 的键?
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; }
}
结果:
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?
使用 key = "#a0.id"
通过索引访问对象是可行的,但我仍然不知道为什么不能通过名称访问对象。
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; }
}
结果:
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?
使用 key = "#a0.id"
通过索引访问对象是可行的,但我仍然不知道为什么不能通过名称访问对象。