如何在使用Oracle 10g的Spring JPA存储库中获取分页结果

huangapple go评论102阅读模式
英文:

How to get paginated result in Spring JPA repository using Oracle 10g

问题

AI_DpEntriesRepository.java

  1. @Repository
  2. public interface AI_DpEntriesRepository extends PagingAndSortingRepository<AI_DpEntries, Long>{
  3. @Query(value = "select a.*,m.description,m.name from AI_DPENTRIES a,"
  4. + "MEDICALHIERARCHY m where PAGEID in (select pageid from PAGES where caseid=8960)"
  5. + " and a.HID=m.ID", nativeQuery = true)
  6. Page<AI_DpEntries> getLabAIDpEntries(Pageable pageable);
  7. }

AIDpEntryServiceImpl.java

  1. @Service
  2. public class AIDpEntryServiceImpl implements AIDpEntryService{
  3. @Autowired
  4. private AI_DpEntriesRepository aiDpEntryRepository;
  5. @Override
  6. @Cacheable("labdpentries")
  7. public Page<AI_DpEntries> getAIDpEntries(int page, int size) {
  8. Pageable pageRequest = PageRequest.of(page, 5);
  9. Page<AI_DpEntries> pageResult = aiDpEntryRepository.getLabAIDpEntries(pageRequest);
  10. List<AI_DpEntries> dpEntries = pageResult.getContent().stream().collect(Collectors.toList());
  11. return new PageImpl<>(dpEntries, pageRequest, pageResult.getTotalElements());
  12. //return aiDpEntryRepository.getLabAIDpEntries();
  13. }
  14. }

出自存储库调用本身的java.sql.SQLSyntaxErrorException: ORA-00904: "A":无效标识符。我做错了什么?如果我将size参数值传递为Integer.MAX_VALUE,它会一次性返回所有记录。但我需要每页获取5条记录。

英文:

AI_DpEntriesRepository.java

  1. public interface AI_DpEntriesRepository extends PagingAndSortingRepository&lt;AI_DpEntries, Long&gt;{
  2. @Query(value = &quot;select a.*,m.description,m.name from AI_DPENTRIES a,&quot;
  3. + &quot;MEDICALHIERARCHY m where PAGEID in (select pageid from PAGES where caseid=8960)&quot;
  4. + &quot; and a.HID=m.ID&quot;,nativeQuery = true)
  5. Page&lt;AI_DpEntries&gt; getLabAIDpEntries(Pageable pageable);
  6. }

AIDpEntryServiceImpl.java

  1. public class AIDpEntryServiceImpl implements AIDpEntryService{
  2. @Autowired
  3. private AI_DpEntriesRepository aiDpEntryRepository;
  4. @Override
  5. @Cacheable(&quot;labdpentries&quot;)
  6. public Page&lt;AI_DpEntries&gt; getAIDpEntries(int page,int size) {
  7. Pageable pageRequest = PageRequest.of(page, 5);
  8. Page&lt;AI_DpEntries&gt; pageResult = aiDpEntryRepository.getLabAIDpEntries(pageRequest);
  9. List&lt;AI_DpEntries&gt; dpEntries = pageResult.getContent().stream().collect(Collectors.toList());
  10. return new PageImpl&lt;&gt;(dpEntries, pageRequest, pageResult.getTotalElements());
  11. //return aiDpEntryRepository.getLabAIDpEntries();
  12. }
  13. }

Getting java.sql.SQLSyntaxErrorException: ORA-00904: "A": invalid identifier
From repository call itself. What am i doing wrong? If i pass size parameter value as Integer.MAX_VALUE it returns all records at single time currently. But my requirement is to get 5 records per page

答案1

得分: 1

解决方案如下 - 需要在本地查询的基础上进行计数查询,计数查询应返回与本地查询返回的行数相同(PS-与提问中的查询相比,查询需要进行轻微更改)

  1. @Repository
  2. public interface AI_DpEntriesRepository extends JpaRepository<AI_DpEntries, Long>{
  3. @Query(value = "select a.*,m.description,m.name from AI_DPENTRIES a,
  4. MEDICALHIERARCHY m where PAGEID in (select pageid from PAGES where caseid=8960) and a.HID=m.ID And (a.REVIEW_IND != 'D' OR a.REVIEW_IND IS NULL)",
  5. countQuery = "select * from AI_DPENTRIES where PAGEID in (select pageid from PAGES where caseid=8960) and (REVIEW_IND != 'D' OR REVIEW_IND IS NULL)", nativeQuery = true)
  6. public Page<AI_DpEntries> getLabAIDpEntries(Pageable pageable);
  7. }
英文:

Solution here - There was a need for count query along with native query which would be returning same no. of rows to as returned by native query(PS-Minor change in query from question asked)

  1. @Repository
  2. public interface AI_DpEntriesRepository extends JpaRepository&lt;AI_DpEntries, Long&gt;{
  3. @Query(value = &quot;select a.*,m.description,m.name from AI_DPENTRIES a,
  4. MEDICALHIERARCHY m where PAGEID in (select pageid from PAGES where caseid=8960) and a.HID=m.ID And (a.REVIEW_IND != &#39;D&#39; OR a.REVIEW_IND IS NULL),
  5. countQuery = &quot;select * from AI_DPENTRIES where PAGEID in (select pageid from PAGES where caseid=8960) and (REVIEW_IND != &#39;D&#39; OR REVIEW_IND IS NULL)&quot;,nativeQuery = true)
  6. public Page&lt;AI_DpEntries&gt; getLabAIDpEntries(Pageable pageable);
  7. }

huangapple
  • 本文由 发表于 2020年10月18日 17:44:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64411859.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定