Winson.Wu
Winson.Wu

Reputation: 135

Spring cache - "Null key returned for cache operation"

@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;
    }
}

It's ERROR When I add 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'

It's doesn't update the cache when I use "save" method.

Upvotes: 4

Views: 9504

Answers (1)

david chi
david chi

Reputation: 246

you could try to replace key with p0

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

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

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

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

Upvotes: 3

Related Questions