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

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

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

问题

AI_DpEntriesRepository.java

@Repository
public interface AI_DpEntriesRepository extends PagingAndSortingRepository<AI_DpEntries, Long>{

	@Query(value = "select a.*,m.description,m.name from AI_DPENTRIES a,"
			+ "MEDICALHIERARCHY m where PAGEID in (select pageid from PAGES where caseid=8960)"
			+ " and a.HID=m.ID", nativeQuery = true)
	Page<AI_DpEntries> getLabAIDpEntries(Pageable pageable);
}

AIDpEntryServiceImpl.java

@Service
public class AIDpEntryServiceImpl implements AIDpEntryService{
	
	@Autowired
	private AI_DpEntriesRepository aiDpEntryRepository;

	@Override
	@Cacheable("labdpentries")
	public Page<AI_DpEntries> getAIDpEntries(int page, int size) {
	 Pageable pageRequest = PageRequest.of(page, 5);
	 Page<AI_DpEntries> pageResult = aiDpEntryRepository.getLabAIDpEntries(pageRequest);
	 List<AI_DpEntries> dpEntries = pageResult.getContent().stream().collect(Collectors.toList());
	 return new PageImpl<>(dpEntries, pageRequest, pageResult.getTotalElements());
	 //return aiDpEntryRepository.getLabAIDpEntries();
	}

}

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

英文:

AI_DpEntriesRepository.java

public interface AI_DpEntriesRepository extends PagingAndSortingRepository&lt;AI_DpEntries, Long&gt;{

	@Query(value = &quot;select a.*,m.description,m.name from AI_DPENTRIES a,&quot;
			+ &quot;MEDICALHIERARCHY m where PAGEID in (select pageid from PAGES where caseid=8960)&quot;
			+ &quot; and a.HID=m.ID&quot;,nativeQuery = true)
	Page&lt;AI_DpEntries&gt; getLabAIDpEntries(Pageable pageable);
}

AIDpEntryServiceImpl.java

public class AIDpEntryServiceImpl implements AIDpEntryService{
	
	@Autowired
	private AI_DpEntriesRepository aiDpEntryRepository;

	@Override
	@Cacheable(&quot;labdpentries&quot;)
	public Page&lt;AI_DpEntries&gt; getAIDpEntries(int page,int size) {
	 Pageable pageRequest = PageRequest.of(page, 5);
	 Page&lt;AI_DpEntries&gt; pageResult = aiDpEntryRepository.getLabAIDpEntries(pageRequest);
	 List&lt;AI_DpEntries&gt; dpEntries = pageResult.getContent().stream().collect(Collectors.toList());
	 return new PageImpl&lt;&gt;(dpEntries, pageRequest, pageResult.getTotalElements());
	 //return aiDpEntryRepository.getLabAIDpEntries();
	}

}

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-与提问中的查询相比,查询需要进行轻微更改)

@Repository
public interface AI_DpEntriesRepository extends JpaRepository<AI_DpEntries, Long>{

@Query(value = "select a.*,m.description,m.name from AI_DPENTRIES a,
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)",
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)

public Page<AI_DpEntries> getLabAIDpEntries(Pageable pageable);

}
英文:

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)

@Repository
public interface AI_DpEntriesRepository extends JpaRepository&lt;AI_DpEntries, Long&gt;{

@Query(value = &quot;select a.*,m.description,m.name from AI_DPENTRIES a,
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),
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)

public Page&lt;AI_DpEntries&gt; getLabAIDpEntries(Pageable pageable);

}

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:

确定