หน้าเว็บ

วันจันทร์ที่ 21 มกราคม พ.ศ. 2556

Multiple Order By PageRequest Spring Data

SQL
...
SELECT FISCALYEAR, SEQUENCENUMBER 
FROM DOCUMENT_SEQUENCE 
ORDER BY FISCALYEAR DESC, SEQUENCENUMBER DESC
...
repository
...
...
public interface DocumentSequenceRepo extends JpaRepository<DocumentSequence, DocumentSequencePK> {

    @Query("SELECT ds.id FROM DocumentSequence ds")
    public Page<DocumentSequencePK> findLatestSequence(Pageable page);
   
}

service
...
...

@Override
public DocumentSequencePK findLatestSequence() {
    String[] sortBy = {"id.fiscalYear", "id.sequenceNumber"}; //**************
    PageRequest pageRequest = new PageRequest(0, 1, Sort.Direction.DESC, sortBy);

    Page<DocumentSequencePK> pk = documentSequenceRepository.findLatestSequence(pageRequest);
    if (pk.getTotalElements() == 0) {
        return new DocumentSequencePK(1L, DateUtils.getFiscalYear(new Date()));
    } else {
        return pk.getContent().get(0);
    }
}
...
...

ไม่มีความคิดเห็น:

แสดงความคิดเห็น