I am able to use Query Cache with Spring Data JPA for my custom query methods like below.
public interface CountryRepository extends JpaRepository<Country, String> {
@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
Country findByCountryName(String countryName);
@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
Country findByCountryCode(String countryCode); }
However, how to add @QueryHints on existing parent methods like findAll()?
Thanks.
Default available methods CrudRepository and PagingAndSortingRepository offer default methods such as: findAll, findAllById, findById, deleteAll, deleteById, save, saveAll.
You can call your function via native query and get result from dual. Note that it won't work if your function is using DML statements. In this case you'll need to use @Modifying annotation over query, but then the function itself must return number due to @Modifying return type restrictions.
findAll(), findOne() etc. are not Query(s). Any caching specifications on the entity take effect in these methods.
For example,
@Cacheable
@Entity
public class User {
}
Originally, there was no support for query hint annotations in default CRUD methods, but apparently it hass been fixed for version 1.6M1:
https://jira.spring.io/browse/DATAJPA-173
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With