2012-02-21 74 views
9

我能夠使用查詢緩存與Spring數據JPA爲我的自定義查詢方法,如下所示。如何在默認的Spring數據JPA方法上添加QueryHints?

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); } 

但是,如何添加@QueryHints現有的父方法,如findAll()?

謝謝。

+0

@oliver -gierke,你有什麼想法如何實現這一目標? – 2012-02-21 14:53:45

+0

我有同樣的問題。我試圖覆蓋它們並添加@QueryHint註釋,但它們被忽略。 – 2014-04-06 23:09:48

+0

@ user791694,請參閱下面的sgp15的答案http://stackoverflow.com/a/10876707/418439 – 2014-04-07 01:12:26

回答

3

findAll(),findOne()等不是Query(s)。這些實體的任何緩存規範都會在這些方法中生效。

例如,

@Cacheable 
@Entity 
public class User { 

} 
+0

另請參見相關的[@緩存設置](http://stackoverflow.com/a/3664293/418439) – 2012-07-20 06:49:54

+0

我有這些設置和findAll它去數據庫。有什麼我需要打開嗎? – 2015-04-07 11:18:21

+0

@ Cacheable似乎對findAll()沒有任何影響。我的理解是,findOne()鏈接到「二級緩存」,但findAll()鏈接到「查詢緩存」。爲了緩存findAll(),我重寫了方法並添加@ queryhints。 – 2016-12-27 21:22:55

相關問題