13

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.

DataNucleus
  • 15,497
  • 3
  • 32
  • 37
Lee Chee Kiam
  • 11,450
  • 10
  • 65
  • 87

2 Answers2

6

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 {

}
sgp15
  • 1,280
  • 8
  • 13
  • I have these set and the for findAll it goes to database. Is there anything I need to turn on ? – Piotr Gwiazda Apr 07 '15 at 11:18
  • @ Cacheable doesn't seem to have any effect on findAll(). My understanding is that findOne() is linked to the "Level 2 cache" but findAll() linked to the "Query cache". To cache the findAll(), i've override the method and add the @ queryhints. – Y.M. Dec 27 '16 at 21:22
1

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

Agustí Sánchez
  • 10,455
  • 2
  • 34
  • 25