Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
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 - Fatal编程技术网

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 如何使用对象';属性作为@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

结果:

org.springframework.expression.spel.SpelEvaluationException: EL1008E:(位置0):在的对象上找不到属性或字段“id” 键入“org.springframework.cache.interceptor.CacheExpressionRootObject” -也许不公开


使用
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; }
}