我们使用Spring Data在分页和可排序的表格中显示计算字段列表:
除了排序支持以外,一切都能正常工作。
要对计算字段进行排序,Spring Data(或JPA?)会自动附加以下语句:
问题是:是否有一种方法或变通方法,可以告诉Spring Data生成自定义的order by语句。一些映射告诉它根据所需的排序字段生成代码?
@Query(value =
"select new com.mycompany.SomeDTO( " +
" c.id as creditAdviceId, " +
" case when c.associationAmount.assignedAmountNet = 0 "+
" then c.netReceivedAmount "+
" else 0 "+
" end "+
" as toAllocateAmount, " +
" c.netReceivedAmount - c.associationAmount.assignedAmountNet "+
" as notAllocatedAmount",
") " +
"from CreditAdvice c where c.debtorCompany.id = :companyId",
countQuery = "select count(c.id) from CreditAdvice c where c.debtorCompany.id = :companyId")
Page<SomeDTO> creditAdviceMonitoring(@Param("companyId") long companyId, Pageable pageable);
除了排序支持以外,一切都能正常工作。
要对计算字段进行排序,Spring Data(或JPA?)会自动附加以下语句:
... order by c.toAllocateAmount desc
这是无效的,因为CreditAdvice实体上不存在c.toAllocateAmount.
但在JPA控制台中测试相同的请求正常工作(由于select语句中的别名):
... order by toAllocateAmount desc
问题是:是否有一种方法或变通方法,可以告诉Spring Data生成自定义的order by语句。一些映射告诉它根据所需的排序字段生成代码?