java—如何使用querydsl在查询中按id删除重复行

whlutmcx  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(566)

我被困在了获取查询中唯一一行代码的查询中。

  1. public JPQLQuery<RecurrenceErrorVO> getQueryErrorRecurrence(Integer companyCode, Collection<SearchModelDTO<Object>> searchModelDTOs) {
  2. //Entities
  3. QRecurrencyEntity qRecurrencyEntity = QRecurrencyEntity.recurrencyEntity;
  4. QSubscriptionEntity qSubscriptionEntity = QSubscriptionEntity.subscriptionEntity;
  5. QRecurrencyDetailsEntity qRecurrencyDetailsEntity = QRecurrencyDetailsEntity.recurrencyDetailsEntity;
  6. QSubcriptionProductEntity qSubcriptionProductEntity = QSubcriptionProductEntity.subcriptionProductEntity;
  7. QArticleEntity qArticleEntity = QArticleEntity.articleEntity;
  8. QCatalogoValorDTO qStatusGeneral = new QCatalogoValorDTO("qStatusGeneral");
  9. QCatalogoValorDTO qrecurrenceType = new QCatalogoValorDTO("qrecurrenceType");
  10. QCatalogoValorDTO qbusinessTypeDTO = new QCatalogoValorDTO("qbusinessTypeDTO");
  11. QCatalogoValorDTO qcausalCatalogType = new QCatalogoValorDTO("qcausalCatalogType");
  12. //data
  13. JPQLQuery<RecurrenceErrorVO> query = from(qRecurrencyDetailsEntity).select(
  14. Projections.bean(RecurrenceErrorVO.class, qRecurrencyEntity.codeRecurrency.as("codeRecurrence"),
  15. qRecurrencyDetailsEntity.codeDetailsTransaction.as("codeRecurrenceDetail"),
  16. qRecurrencyEntity.statusGeneralValue.as("statusGeneralValue"),
  17. qRecurrencyEntity.processDate.as("dateRecurrence"),
  18. qRecurrencyDetailsEntity.transactionDate.as("dateTransaction"),
  19. qSubscriptionEntity.contractIdentifier,
  20. qSubcriptionProductEntity.articleEntity.itemDescription.as("productName"),
  21. qSubscriptionEntity.numberDocumentClient.as("clientDocNumber"),
  22. qSubscriptionEntity.customerName.as("clientName"),
  23. qSubscriptionEntity.subscriptionValue.as("subscriptionValue"),
  24. qSubscriptionEntity.statusSubscriptionValue,
  25. qStatusGeneral.nombreCatalogoValor.as("statusGeneralDescription"),
  26. qRecurrencyEntity.originRecurrenceValue.as("originRecurrenceValue"),
  27. qRecurrencyEntity.value,
  28. qRecurrencyEntity.status,
  29. qRecurrencyDetailsEntity.causalValue.as("codeCausalValue"),
  30. qRecurrencyDetailsEntity.causalType.as("codeCausalType"),
  31. qbusinessTypeDTO.nombreCatalogoValor.as("marca"),
  32. qcausalCatalogType.nombreCatalogoValor.as("causalValue"),
  33. qrecurrenceType.nombreCorto.as("nombreCorto"),
  34. qrecurrenceType.nombreCatalogoValor.as("transactionType")))
  35. //join
  36. query.leftJoin(qRecurrencyDetailsEntity.recurrencyEntity, qRecurrencyEntity)
  37. .leftJoin(qRecurrencyEntity.subscriptionEntity, qSubscriptionEntity)
  38. .leftJoin(qRecurrencyEntity.subcriptionProductEntity, qSubcriptionProductEntity)
  39. .innerJoin(qSubcriptionProductEntity.articleEntity, qArticleEntity)
  40. .innerJoin(qRecurrencyEntity.statusGeneral, qStatusGeneral)
  41. .innerJoin(qSubcriptionProductEntity.businessTypeDTO, qbusinessTypeDTO)
  42. .innerJoin(qRecurrencyDetailsEntity.recurrenceType, qrecurrenceType)
  43. .innerJoin(qRecurrencyDetailsEntity.causalCatalogType, qcausalCatalogType)
  44. ;
  45. BooleanBuilder where = new BooleanBuilder();
  46. where.and(qRecurrencyEntity.statusGeneralValue.ne(SirConstants.RECURRENCE_STATUS_FIN));
  47. where.and(qRecurrencyEntity.statusGeneralValue.ne(SirConstants.RECURRENCE_STATUS_SIN_CUPO));
  48. where.and(qRecurrencyEntity.statusGeneralValue.ne(SirConstants.RECURRENCE_STATUS_FIN_WITH_OBSERVATION));
  49. where.and(qRecurrencyDetailsEntity.statusRecurrencyDetailsValue.ne(SirConstants.ZERO.toString()));
  50. where.and(qRecurrencyEntity.originRecurrenceValue.eq(SirConstants.RECURRENCE_ORIGIN_INTERNO));
  51. where.and(qRecurrencyEntity.companyCode.eq(companyCode));
  52. where.and(qRecurrencyEntity.status.isTrue());
  53. SearchModelUtil.addDynamicWhere(searchModelDTOs, where, RecurrencyEntity.class, "recurrencyEntity");
  54. query.where(where);
  55. query.orderBy(qRecurrencyEntity.codeRecurrency.desc(), qRecurrencyDetailsEntity.codeDetailsTransaction.desc());
  56. return query;
  57. }

这是我当前的代码,它返回值​​作为下一张table

  1. CODERECURRENCY|CODEDCURRENCYDETAILS|STATUSGENERALVALUE|PROCESSDATE |TRANSACTIONDATE
  2. --------------|--------------------|------------------|-------------------|-------------------
  3. 16202| 14510|ERR |2020-10-23 12:08:26|2020-11-27 16:53:57
  4. 16202| 14094|ERR |2020-10-23 12:08:26|2020-10-23 12:08:47
  5. 16202| 14093|ERR |2020-10-23 12:08:26|2020-10-23 12:08:41
  6. 16201| 14088|EXR |2020-10-22 23:51:58|2020-10-22 23:53:43
  7. 16201| 14087|EXR |2020-10-22 23:51:58|2020-10-22 23:53:37
  8. 16201| 14083|EXR |2020-10-22 23:51:58|2020-10-22 23:53:15
  9. 16201| 14082|EXR |2020-10-22 23:51:58|2020-10-22 23:53:09
  10. 16201| 14078|EXR |2020-10-22 23:51:58|2020-10-22 23:52:47
  11. 16201| 14077|EXR |2020-10-22 23:51:58|2020-10-22 23:52:41
  12. 16201| 14073|EXR |2020-10-22 23:51:58|2020-10-22 23:52:19
  13. 16201| 14072|EXR |2020-10-22 23:51:58|2020-10-22 23:52:13
  14. 16123| 13675|ERR |2020-10-01 17:06:17|2020-10-01 17:06:28
  15. 16050| 13511|ERR |2020-09-21 14:11:31|2020-09-21 14:11:31
  16. 16043| 13470|EXR |2020-09-16 10:04:20|2020-09-16 10:07:00

需要一行一行的代码重现,我用代码删除重复行,但如果我把jpqlquery转换成一个列表,这个改变结果的值显示在webservice,我需要使用分页。
这段代码显示了查询的用法。

  1. private PageResultVO<RecurrenceErrorVO> findPagedRE(JPQLQuery<RecurrenceErrorVO> query, Pageable pageable) {
  2. List<RecurrenceErrorVO> list = Objects.requireNonNull(getQuerydsl()).applyPagination(
  3. pageable, query).fetch();
  4. Page<RecurrenceErrorVO> page = PageableExecutionUtils.getPage(list,
  5. pageable,
  6. query::fetchCount);
  7. return new PageResultVO<>(page.getContent(), page.getPageable(), page.getTotalElements());
  8. }

我将这段代码作为带分页的webservice使用。
我该怎么办?

kg7wmglp

kg7wmglp1#

您需要将聚合函数与GROUPBY子句一起使用(不能与 query::fetchCount 在querydsl for jpa中)或具有窗口函数的不同元组(需要注册自定义函数)。即使这样,你也可能会在你的投影中与实体投影斗争。例如,你不能 MAX(someEntity) 获取实体 MAX(someEntity.id) .
使用普通的hibernate和querydsl无法做到这一点,但是可以使用针对querydsl的blaze持久性集成来做到这一点。使用这个hibernate扩展,您可以使用窗口函数或 fetchCount 在指定 GROUP BY 或者 HAVING 条款。您还可以使用blaze persistence cte或子查询横向连接来创建查询。

相关问题