【问题标题】:How to use an objects' property as the key for @CachePut?如何使用对象的属性作为@CachePut 的键?
【发布时间】:2016-10-01 02:27:09
【问题描述】:
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): 在 的对象上找不到属性或字段“id” 输入“org.springframework.cache.interceptor.CacheExpressionRootObject” - 也许不公开?

【问题讨论】:

  • 试试#entity.id..
  • org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Property or field 'id' cannot be found on null
  • 显然你在持久化一个空实体
  • 不,我不是。只是#entity.id 无法以某种方式解决。当我删除 @CachePut 注释时,案例工作正常。

标签: java spring caching spring-cache spelevaluationexception


【解决方案1】:

使用key = "#a0.id" 按索引访问对象有效,但我仍然不知道为什么不能按名称访问对象。

【讨论】:

  • 你可以用#id代替
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
  • 1970-01-01
  • 1970-01-01
  • 2019-08-27
  • 1970-01-01
  • 2021-10-23
  • 2012-06-06
相关资源
最近更新 更多