Java 如何使用对象';属性作为@CachePut的键? 公共接口MyRespository扩展了CrudRespository{ @CachePut(value=“mycache”,key=“id”) @凌驾 公众储蓄(S实体); } @实体 公共类MyEntity{ @身份证 私人长id; public Long getId(){return id;} public void setId(长id){this.id=id;} }
结果: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(位置0):在的对象上找不到属性或字段“id” 键入“org.springframework.cache.interceptor.CacheExpressionRootObject” -也许不公开Java 如何使用对象';属性作为@CachePut的键? 公共接口MyRespository扩展了CrudRespository{ @CachePut(value=“mycache”,key=“id”) @凌驾 公众储蓄(S实体); } @实体 公共类MyEntity{ @身份证 私人长id; public Long getId(){return id;} public void setId(长id){this.id=id;} },java,spring,caching,spring-cache,spelevaluationexception,Java,Spring,Caching,Spring Cache,Spelevaluationexception,结果: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(位置0):在的对象上找不到属性或字段“id” 键入“org.springframework.cache.interceptor.CacheExpressionRootObject” -也许不公开 使用key=“#a0.id”通过索引访问对象是有效的,但我仍然不知道为什么不能通过名称访问对象。试试#entity.id。org.springframewo
使用
key=“#a0.id”
通过索引访问对象是有效的,但我仍然不知道为什么不能通过名称访问对象。试试#entity.id
。org.springframework.expression.spel.SpelEvaluationException:EL1007E:(位置0):在null上找不到属性或字段“id”
显然您正在持久化null实体否我没有。很明显,#entity.id
无法以某种方式解析。当我删除@CachePut
注释时,案例工作正常。您可以使用#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; }
}