spring-data-jpa icon indicating copy to clipboard operation
spring-data-jpa copied to clipboard

Using @NaturalId for cache

Open jhhong0509 opened this issue 4 years ago • 5 comments

I'm using @NaturalId annotation in my User.email. I expected that my findByEmail(String email) would enable to search in first level cache or second level cache. But in log, cache does not work as I think. Every findByEmail method were access DB.

I used bySimpleNaturalId() for my purpose, but it's very hassle. Please make @NaturalId access to cache enable

jhhong0509 avatar Sep 26 '21 00:09 jhhong0509

This is an interesting proposal. Lets wait until it is actually part of JPA: https://github.com/eclipse-ee4j/jpa-api/issues/320

We try to avoid implementation specifics in Spring Data JPA.

schauder avatar Sep 27 '21 14:09 schauder

Is it really necessary having to go through findByNaturalId()? If you already mark the natural id foo field as @NaturalId, perhaps it can be some way to make loadByFoo automatically marked internally as a NaturalId, and through reflection, cache it

nightswimmings avatar Mar 01 '23 23:03 nightswimmings

Yes, because Spring Data JPA certainly won't get into caching itself.

schauder avatar Mar 02 '23 09:03 schauder

I meant caching it though hinting the JPA Provider. Idk, it's just that it feels so antifunctional to use findByNaturalId() when findByEmail(), findByISBN() or findByName() is so readable :,(

nightswimmings avatar Mar 02 '23 23:03 nightswimmings