0

I have this abstract repository class:

public abstract class FirestoreRepository<T> {
    protected Firestore db;

    protected FirestoreRepository(Firestore db) {
        this.db = db;
    }

    @Cacheable(value = , key = "#id")
    public Optional<T> findDocumentWithId(@Nonnull String id) throws InterruptedException, ExecutionException {
        DocumentSnapshot doc = db.collection(getCollection()).document(id).get().get();
        return Optional.ofNullable(doc.exists() ? doc.toObject(getEntityClass()) : null);
    }

    public abstract @Nonnull String getCollection();


...
}

And I do have multiple repository implementations that inherit from the above class.

@Service
public class UserRepository extends FirestoreRepository<User> {

    @Autowired
    public UserRepository(Firestore db) {
        super(db);
    }
    
    @Override
    public @Nonnull String getCollection() {
        return FirestoreConstants.USERS_COLLECTIONS_NAME;
    }


...
}

I want to call getCollection() method, in here like this for example: @Cacheable(value = "#getCollection()", key = "#id")

1 Answer 1

0
 @Cacheable(value = "{#root.target.getCollection()}", key = "#id")

#root.target.getCollection() to call the getCollection() method. In this case, root target object would be the UserRepository object.
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?
Thanks for the answer. But this is not working.

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.