使用 JpaRepository Spring-data-jpa 对子列表总计数进行排序
Sort On Child List total count using JpaRepository Spring-data-jpa
我有对实体进行分页和排序的需求。
@Entity
@Table(name = "CATEGORY", catalog = "")
public class CategoryEntity {
private CategoryEntity categoryByParentCategoryId;
private Set<CategoryEntity> categoriesByCategoryId;
@ManyToOne(fetch = FetchType.LAZY,optional = false, cascade = CascadeType.PERSIST)
@JoinColumn(name = "PARENT_CATEGORY_ID", referencedColumnName = "CATEGORY_ID")
public CategoryEntity getCategoryByParentCategoryId() {
return categoryByParentCategoryId;
}
public void setCategoryByParentCategoryId(CategoryEntity categoryByParentCategoryId) {
this.categoryByParentCategoryId = categoryByParentCategoryId;
}
@OneToMany(mappedBy = "categoryByParentCategoryId", cascade = CascadeType.PERSIST)
public Set<CategoryEntity> getCategoriesByCategoryId() {
return categoriesByCategoryId;
}
public void setCategoriesByCategoryId(Set<CategoryEntity> categoriesByCategoryId) {
this.categoriesByCategoryId = categoriesByCategoryId;
}
从 this link 和其他堆栈溢出答案中,我发现我可以使用 Paging Request
like
进行排序和分页
Pageable size = new PageRequest(page, paginationDTO.getSize(),Sort.Direction.ASC, "id");
我的问题是我有一个 self join
父子关系,如上图所示,我需要根据子项的数量对父项进行排序,如下所示。
这里的Number of SubCategories
是categoriesByCategoryId
的大小。我需要在 PageRequest
中传递什么来代替 id
以根据子列表的大小进行排序。
PS。该模型有更多字段,但为了简短的问题,我只发布了相关字段
通过 this answer 我能够通过使用自定义查询实现要求,JPARepository 中的方法看起来像
@Query(
value = "select c from CategoryEntity c " +
" WHERE LOWER(c.categoryNameEn) LIKE LOWER(CONCAT('%',?2, '%')) AND activeInd = ?1 " +
"AND c.categoryByParentCategoryId is null" +
" Order By c.categoriesByCategoryId.size desc",
countQuery = "select count(c) from CategoryEntity c " +
" WHERE LOWER(c.categoryNameEn) LIKE LOWER(CONCAT('%',?2, '%')) AND activeInd = ?1" +
" AND c.categoryByParentCategoryId is null"
)
Page<CategoryEntity> findAllActiveCategoriesByCategoriesByCategoryIdCountDesc(String activeInd, String categoryNameEn, Pageable pageable);
分页详细信息需要计数查询。
我有对实体进行分页和排序的需求。
@Entity
@Table(name = "CATEGORY", catalog = "")
public class CategoryEntity {
private CategoryEntity categoryByParentCategoryId;
private Set<CategoryEntity> categoriesByCategoryId;
@ManyToOne(fetch = FetchType.LAZY,optional = false, cascade = CascadeType.PERSIST)
@JoinColumn(name = "PARENT_CATEGORY_ID", referencedColumnName = "CATEGORY_ID")
public CategoryEntity getCategoryByParentCategoryId() {
return categoryByParentCategoryId;
}
public void setCategoryByParentCategoryId(CategoryEntity categoryByParentCategoryId) {
this.categoryByParentCategoryId = categoryByParentCategoryId;
}
@OneToMany(mappedBy = "categoryByParentCategoryId", cascade = CascadeType.PERSIST)
public Set<CategoryEntity> getCategoriesByCategoryId() {
return categoriesByCategoryId;
}
public void setCategoriesByCategoryId(Set<CategoryEntity> categoriesByCategoryId) {
this.categoriesByCategoryId = categoriesByCategoryId;
}
从 this link 和其他堆栈溢出答案中,我发现我可以使用 Paging Request
like
Pageable size = new PageRequest(page, paginationDTO.getSize(),Sort.Direction.ASC, "id");
我的问题是我有一个 self join
父子关系,如上图所示,我需要根据子项的数量对父项进行排序,如下所示。
这里的Number of SubCategories
是categoriesByCategoryId
的大小。我需要在 PageRequest
中传递什么来代替 id
以根据子列表的大小进行排序。
PS。该模型有更多字段,但为了简短的问题,我只发布了相关字段
通过 this answer 我能够通过使用自定义查询实现要求,JPARepository 中的方法看起来像
@Query(
value = "select c from CategoryEntity c " +
" WHERE LOWER(c.categoryNameEn) LIKE LOWER(CONCAT('%',?2, '%')) AND activeInd = ?1 " +
"AND c.categoryByParentCategoryId is null" +
" Order By c.categoriesByCategoryId.size desc",
countQuery = "select count(c) from CategoryEntity c " +
" WHERE LOWER(c.categoryNameEn) LIKE LOWER(CONCAT('%',?2, '%')) AND activeInd = ?1" +
" AND c.categoryByParentCategoryId is null"
)
Page<CategoryEntity> findAllActiveCategoriesByCategoriesByCategoryIdCountDesc(String activeInd, String categoryNameEn, Pageable pageable);
分页详细信息需要计数查询。