java - 如何在 JPA 中找到与规范和可分页关系密切的所有内容?
问题描述
我反对多对多的关系product
。kind
我可以通过以下方式获得具有热切关系的产品:
@Query(
value = "select distinct product from Product product left join fetch product.kinds",
countQuery = "select count(distinct product) from Product product"
)
Page<Product> findAllWithEagerRelationships(Pageable pageable);
或按规格和可分页的产品:
@Transactional(readOnly = true)
public Page<Product> findByCriteria(ProductCriteria criteria, Pageable page) {
log.debug("find by criteria : {}, page: {}", criteria, page);
final Specification<Product> specification = createSpecification(criteria);
return productRepository.findAll(specification, page);
}
但是当我尝试将它们合二为一时,
@Query(
value = "select distinct product from Product product left join fetch product.kinds",
countQuery = "select count(distinct product) from Product product"
)
Page<Product> findAll(Specification<Product> specification, Pageable pageable);
我收到错误:
Caused by: org.hibernate.HibernateException: firstResult/maxResults specified with collection fetch. In memory pagination was about to be applied. Failing because 'Fail on pagination over collection fetch' is enabled.
所以我该怎么做?
解决方案
我找到了临时解决方案。我将@ManyToMany
实体类中的注释更改为
@ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
然后我的代码工作异常。如果你想试试这个。但我不确定这个解决方案的性能。我需要确认一下。
更新:此解决方案可以解决加载数据问题,但我将无法编辑数据
推荐阅读
- c# - 标签使用 RightToLeft 错误地显示标点符号
- java - Java 客户端 OCSP 抛出“无法确定吊销状态”
- python - 从 sqlite 数据库中以整数形式检索某些内容
- javascript - 为什么我的倍数函数返回 NaN?
- python - 乘客应用程序错误:内部错误
- javascript - 如何访问护照中的整个用户对象
- javascript - 如何将 JSON 文件转换为 JS 数组
- google-cloud-firestore - SwiftUI 和 Picker 和 FireStore 返回文档 ID 不落
- flask - url_for 的烧瓶蓝图问题
- wordpress - 触发高级自定义字段 (ACF) 'acf/save_post' 操作