【问题标题】:Cache with JpaRepository使用 JpaRepository 缓存
【发布时间】:2015-01-06 09:38:52
【问题描述】:

您好,我已经通过以下方式扩展了 JpaRepository 接口。

public interface StudentRepository extends JpaRepository<Student,Integer>
{
@Query(value= "SELECT s.id FROM student as s where s.createdat >  ADDDATE(CURRENT_DATE, :maxage ", nativeQuery = true )
public List<Integer> findWaitingStudentIds(@Param("maxage")int maxAge);
}

这是Entity 类。

@Entity(name="student ")
public class Student implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(unique=true, nullable=false)
    private Integer  id;
    @Temporal(TemporalType.TIMESTAMP)
    @Column(updatable = false,insertable = false)
    private Date createdat;  
}

我想为“List findWaitingStudentIds”方法添加缓存。我怎样才能做到这一点?

【问题讨论】:

  • here也许这有帮助。

标签: java spring hibernate


【解决方案1】:

我可以从这个 StackOverflow 问题中复制粘贴我的答案:

How should I implement a cache object/system in Spring?

Spring 在 3.x RELEASE 中引入了 Cache 的抽象。你可以阅读 在官方 Spring 文档中关于它(该网站今天关闭 出于某种原因:)),或者例如在这篇文章中。

http://dzone.com/articles/spring-cache-abstraction-0

有了这个抽象,启用缓存所需要做的就是添加 您的服务的一些注释,例如

为缓存增加价值

@Cacheable("customers")
public Customer findCustomer(long customerId) {...}

将值移除到缓存中

@CacheEvict(value="customer", allEntries = true)
public void removeAllCustomers(long customerId) {...}

【讨论】:

    【解决方案2】:

    您可以考虑阅读Hibernate Reference Manual。当我第一次使用它时,它对我帮助很大。它真正阐明了概念及其工作方式。

    StackOverflow 中还有其他一些答案:Hibernate Cache Reference

    网上也有一些快速教程:

    希望这些信息对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-14
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多