首页 > 解决方案 > 使用 Spring Data JPA 和 Stream 的休眠查询缓存不起作用

问题描述

我在我的 Spring Data JPA 存储库上使用 Hibernate 查询缓存,如果我使用 List 作为返回类型,一切正常,如果我使用 Stream 似乎休眠不缓存结果:

不工作的代码:

@Query("select E from Entity E where E.id = :id E.name asc")
@QueryHints({@QueryHint(name = "org.hibernate.cacheable", value = "true")})
Stream<Entity> getProvinceList(@Param("id") String id);

工作代码:

@Query("select E from Entity E where E.id = :id E.name asc")
@QueryHints({@QueryHint(name = "org.hibernate.cacheable", value = "true")})
List<Entity> getProvinceList(@Param("id") String id);

在客户端代码中关闭或不关闭流没有区别。

有人有同样的问题吗?实际上我解决了使用列表并将其转换为客户端代码中的流。

标签: javahibernatespring-data-jpajava-streamquery-cache

解决方案


推荐阅读