Spring JPA Hibernate查询缓存不起作用

编程入门 行业动态 更新时间:2024-10-13 10:23:46
本文介绍了Spring JPA Hibernate查询缓存不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用Spring Boot 1.4.1和spring-boot-starter-data-jpa

I use Spring Boot 1.4.1 AND spring-boot-starter-data-jpa

查询我的自定义方法(例如'findByName(String name)')时,它不会缓存.

When query my custom method like 'findByName(String name)' it's not cache.

但是当查询默认方法(例如'findOne(Interger id)')时,它就起作用了.

But when query default method like 'findOne(Interger id)' it's work.

application.properties:

application.properties:

spring.jpa.properties.hibernate.cache.use_query_cache=true

存储库:

@Repository public interface AuthorRepository extends CrudRepository<Author, Integer> { Author findByName(String name); }

测试:

public class RepositoryTests { @Autowired private AuthorRepository authorRepository; @Test @Transactional public void test() { authorRepository.save(new Author("admin")); // ***Not work. query **5** times. Author author = authorRepository.findByName("admin"); author = authorRepository.findByName("admin"); author = authorRepository.findByName("admin"); author = authorRepository.findByName("admin"); author = authorRepository.findByName("admin"); // ***It's work. query **1** times. Author author = authorRepository.findOne(1); author = authorRepository.findOne(1); author = authorRepository.findOne(1); author = authorRepository.findOne(1); author = authorRepository.findOne(1); } }

推荐答案

@Repository public interface AuthorRepository extends CrudRepository<Author, Integer> { @QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") }) Author findByName(String name); }

应该做到这一点.(注意:因为您已经扩展了CrudRepository,所以不需要 @Repository )

Should do the trick. (Note: the @Repository is not needed because you already extend CrudRepository)

更多推荐

Spring JPA Hibernate查询缓存不起作用

本文发布于:2023-11-26 08:40:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633408.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   不起作用   Spring   JPA   Hibernate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!