4
@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.

4
  • Can you post the code for UserApi class as well ? Commented Dec 25, 2016 at 7:59
  • I posted the code for UserApi class 。 Commented Dec 25, 2016 at 8:07
  • Well do you have a user with a null user ID? The fact that you have both getId and getUserId sounds worrying... which is the identifier for the user? Commented Dec 25, 2016 at 9:08
  • userId identifier for the user Commented Dec 25, 2016 at 11:30

1 Answer 1

3

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.