Spring cache - "为缓存操作返回Null键"

时间:2016-12-25 07:56:31

标签: java spring caching

@Transactional
@Repository
@RepositoryDefinition(domainClass = UserApi.class, idClass = String.class)
public interface UserApiRepository{

    @Cacheable(value="byUserId",key="#userId")
    List<UserApi> findByUserId(String userId);

    @CacheEvict(value="byUserId",key="#userApi.userId")
    void save(UserApi userApi);
}

UsrApi.java

@Entity
@Table(name = "user_api")
public class UserApi {
    private String id;
    private String userId;
    private String api;
    private String platform;
    private String apiType;

    @Id
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getApi() {
        return api;
    }

    public void setApi(String api) {
        this.api = api;
    }

    public String getPlatform() {
        return platform;
    }

    public void setPlatform(String platform) {
        this.platform = platform;
    }

    public String getApiType() {
        return apiType;
    }

    public void setApiType(String apiType) {
        this.apiType = apiType;
    }
}

错误我添加key="#userId"

java.lang.IllegalArgumentException: Null key returned for cache operation
(maybe you are using named params on classes without debug info?)
Builder[public abstract java.util.List com.awinson.repository.UserApiRepository.findByUserId(java.lang.String)]
caches=[byUserId] | key='#userId' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'

当我使用&#34; save&#34;时,它不会更新缓存。方法

1 个答案:

答案 0 :(得分:0)

你可以尝试用p0替换密钥

@Transactional     @Repository     @RepositoryDe​​finition(domainClass = UserApi.class,idClass = String.class)     公共接口UserApiRepository {

    @Cacheable(value="byUserId",key="#p0")
    List<UserApi> findByUserId(String userId);

    @CacheEvict(value="byUserId",key="#p0.userId")
    void save(UserApi userApi);
}

参考 Spring Cache Abstraction VS interfaces VS key param ("Null key returned for cache operation" error)