Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add QueryHints on Default Spring Data JPA Methods?

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.

like image 668
Lee Chee Kiam Avatar asked Feb 21 '12 02:02

Lee Chee Kiam


People also ask

What are the default methods in JPA repository?

Default available methods CrudRepository and PagingAndSortingRepository offer default methods such as: findAll, findAllById, findById, deleteAll, deleteById, save, saveAll.

How do you call a spring data function in JPA?

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.


2 Answers

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 {

}
like image 136
sgp15 Avatar answered Sep 29 '22 07:09

sgp15


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

like image 43
Agustí Sánchez Avatar answered Sep 30 '22 07:09

Agustí Sánchez