【问题标题】:Spring boot with hibernate first level cache带有休眠一级缓存的 Spring Boot
【发布时间】:2021-08-07 08:18:15
【问题描述】:

我在同一个方法中调用了两次CrudRepository#findById,它显示在控制台第一个选择语句中。 为什么第二个 select 语句没有显示在第二个方法调用中。

这里是服务代码:

@Service
@RequiredArgsConstructor
public class AccountService {
    private final AccountReporitory accountReporitory;

    public void find() {
        final Account account1 = accountReporitory.findById(1L).get();
        final Account account2 = accountReporitory.findById(1L).get();
    }
}

这是存储库代码:

public interface AccountReporitory extends JpaRepository<Account, Long> {
}

【问题讨论】:

标签: spring-boot hibernate jpa caching spring-data-jpa


【解决方案1】:

当您第一次调用findById(1L) 时,生成的实体将被放置在会话的一级缓存中。在同一会话中对具有相同 id 的任何后续调用 findById() 将导致实体管理器从一级缓存中检索实体,而不是执行数据库查询。这是一级缓存的主要用途之一。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 2010-10-20
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多