使用本机查询时如何在 Spring Data Jpa 中获取 Page<Object[]> 之类的东西
How to get Page<Object[]> like stuff in Spring Data Jpa when using native query
我卡在了一个点上。我有一个正在使用的查询,如下所示
@Query(value="SELECT bha.activity_code_id, ac.activity_code_full_name, pt.plan_name " +
" FROM bp_bid_history_activity bha, activity_code ac, plan_template pt where rownum < 201", nativeQuery = true)
List<Object[]> getContractReceivedAddedActivityCodes(@Param("bidId")Long bidId, @Param("historyId")Long historyId, Pageable pageable);
// Trying to achieve this
@Query(value="SELECT bha.activity_code_id, ac.activity_code_full_name, pt.plan_name " +
" FROM bp_bid_history_activity bha, activity_code ac, plan_template pt where rownum < 201", nativeQuery = true)
Page<Object[]> getContractReceivedAddedActivityCodesPageable(@Param("bidId")Long bidId, @Param("historyId")Long historyId, Pageable pageable);
实际上为了查看结果,我将查询限制为 200 个结果。因为实际查询不是 return 结果。在实际查询中有适当的 where 子句。但无论如何。
下面是我的使用方法
//@Test
public void getContractReceivedAddedActivityCodes() {
Long bidId = 178820L;
Long historyId = 783520L;
Pageable pageable = PageRequest.of(0, 10, Sort.by("activity_code_id"));
List<Object[]> addedActivitiesList = bpBidActivityRepository.getContractReceivedAddedActivityCodes(bidId, historyId, pageable);
List<ContractReceivedActivityCodesModel> addedActivityCodes = getContractReceivedActivityCodesModel(addedActivitiesList);
printList(addedActivityCodes);
}
private List<ContractReceivedActivityCodesModel> getContractReceivedActivityCodesModel(List<Object[]> activitiesList) {
List<ContractReceivedActivityCodesModel> activityCodes = null;
if (CollectionUtils.isNotEmpty(activitiesList)) {
activityCodes = activitiesList.stream()
.map(ContractReceivedActivityCodesModel::new).collect(Collectors.toList());
}
return activityCodes;
}
public class ContractReceivedActivityCodesModel {
private Long activeCodeId;
private String activityCodeFullName;
private String planName;
public ContractReceivedActivityCodesModel(Object[] object) {
if (ArrayUtils.isNotEmpty(object)) {
BigDecimal activeCodeId = (BigDecimal) object[0];
if (activeCodeId != null) {
this.activeCodeId = activeCodeId.longValue();
}
this.activityCodeFullName = (String) object[1];
this.planName = (String) object[2];
}
}
}
查询 return 有 200 个结果。现在的问题是,当 List<Object[]>
查询运行时,我只得到 10 个结果。因为我传递的是 10 页大小。但我也想获得总记录大小。所以我可以在 UI 上显示分页,显示 200 条记录中的 10 条,显示 200 条记录中的 30 条。
当我们使用 Page<Person>
或 Page<T>
return 类型时,我们也会得到总计数。在这种情况下我怎样才能得到总数。或者我想得到像 Page<Object[]>
这样的东西。我尝试了 Page<List<Object[]>>
、Page<Object[]>
之类的操作,但出现错误。
谢谢
您必须提供一个 countQuery
@Query(value="SELECT bha.activity_code_id, ac.activity_code_full_name, pt.plan_name " +
" FROM bp_bid_history_activity bha, activity_code ac, plan_template pt where rownum < 201",
countQuery="SELECT count(*) " +
" FROM bp_bid_history_activity bha, activity_code ac, plan_template pt where rownum < 201"
nativeQuery = true)
Page<Object[]> getContractReceivedAddedActivityCodesPageable(@Param("bidId")Long bidId, @Param("historyId")Long historyId, Pageable pageable);
我卡在了一个点上。我有一个正在使用的查询,如下所示
@Query(value="SELECT bha.activity_code_id, ac.activity_code_full_name, pt.plan_name " +
" FROM bp_bid_history_activity bha, activity_code ac, plan_template pt where rownum < 201", nativeQuery = true)
List<Object[]> getContractReceivedAddedActivityCodes(@Param("bidId")Long bidId, @Param("historyId")Long historyId, Pageable pageable);
// Trying to achieve this
@Query(value="SELECT bha.activity_code_id, ac.activity_code_full_name, pt.plan_name " +
" FROM bp_bid_history_activity bha, activity_code ac, plan_template pt where rownum < 201", nativeQuery = true)
Page<Object[]> getContractReceivedAddedActivityCodesPageable(@Param("bidId")Long bidId, @Param("historyId")Long historyId, Pageable pageable);
实际上为了查看结果,我将查询限制为 200 个结果。因为实际查询不是 return 结果。在实际查询中有适当的 where 子句。但无论如何。
下面是我的使用方法
//@Test
public void getContractReceivedAddedActivityCodes() {
Long bidId = 178820L;
Long historyId = 783520L;
Pageable pageable = PageRequest.of(0, 10, Sort.by("activity_code_id"));
List<Object[]> addedActivitiesList = bpBidActivityRepository.getContractReceivedAddedActivityCodes(bidId, historyId, pageable);
List<ContractReceivedActivityCodesModel> addedActivityCodes = getContractReceivedActivityCodesModel(addedActivitiesList);
printList(addedActivityCodes);
}
private List<ContractReceivedActivityCodesModel> getContractReceivedActivityCodesModel(List<Object[]> activitiesList) {
List<ContractReceivedActivityCodesModel> activityCodes = null;
if (CollectionUtils.isNotEmpty(activitiesList)) {
activityCodes = activitiesList.stream()
.map(ContractReceivedActivityCodesModel::new).collect(Collectors.toList());
}
return activityCodes;
}
public class ContractReceivedActivityCodesModel {
private Long activeCodeId;
private String activityCodeFullName;
private String planName;
public ContractReceivedActivityCodesModel(Object[] object) {
if (ArrayUtils.isNotEmpty(object)) {
BigDecimal activeCodeId = (BigDecimal) object[0];
if (activeCodeId != null) {
this.activeCodeId = activeCodeId.longValue();
}
this.activityCodeFullName = (String) object[1];
this.planName = (String) object[2];
}
}
}
查询 return 有 200 个结果。现在的问题是,当 List<Object[]>
查询运行时,我只得到 10 个结果。因为我传递的是 10 页大小。但我也想获得总记录大小。所以我可以在 UI 上显示分页,显示 200 条记录中的 10 条,显示 200 条记录中的 30 条。
当我们使用 Page<Person>
或 Page<T>
return 类型时,我们也会得到总计数。在这种情况下我怎样才能得到总数。或者我想得到像 Page<Object[]>
这样的东西。我尝试了 Page<List<Object[]>>
、Page<Object[]>
之类的操作,但出现错误。
谢谢
您必须提供一个 countQuery
@Query(value="SELECT bha.activity_code_id, ac.activity_code_full_name, pt.plan_name " +
" FROM bp_bid_history_activity bha, activity_code ac, plan_template pt where rownum < 201",
countQuery="SELECT count(*) " +
" FROM bp_bid_history_activity bha, activity_code ac, plan_template pt where rownum < 201"
nativeQuery = true)
Page<Object[]> getContractReceivedAddedActivityCodesPageable(@Param("bidId")Long bidId, @Param("historyId")Long historyId, Pageable pageable);