我有一个spring-boot应用程序并使用JPA与PostgreSQL交谈。我有自定义查询
SELECT * FROM public.user
INNER JOIN public.company
ON (public.user.company_id = public.company.id)
INNER JOIN public.truck
ON (public.user.id = public.truck.user_id)
INNER JOIN public.job
ON (public.truck.id = public.job.truck_id)
WHERE public.company.id = 1
ORDER BY 1 USING < LIMIT 10 OFFSET 0
当我在SQL编辑器(如DBVisualizer或pgAdmin)中运行上述查询时,我收到订单,当我将其反转时
SELECT * FROM public.user
INNER JOIN public.company
ON (public.user.company_id = public.company.id)
INNER JOIN public.truck
ON (public.user.id = public.truck.user_id)
INNER JOIN public.job
ON (public.truck.id = public.job.truck_id)
WHERE public.company.id = 1
ORDER BY 1 USING > LIMIT 10 OFFSET 0
我仍然得到了正确的结果,但是来自我的代码,就像
@Query(nativeQuery = true, value = "SELECT * FROM public.user" +
" INNER JOIN public.company" +
" ON (public.user.company_id = public.company.id)" +
" INNER JOIN public.truck" +
" ON (public.user.id = public.truck.user_id)" +
" INNER JOIN public.job" +
" ON (public.truck.id = public.job.truck_id)" +
" WHERE public.company.id = :companyId" +
" ORDER BY :columnNumber USING > LIMIT :pageSize OFFSET :offset")
List<Object[]> findJobsASC(@Param("companyId") Long companyId, @Param("pageSize") int pageSize, @Param("columnNumber") int columnNumber, @Param("offset") int offset);
@Query(nativeQuery = true, value = "SELECT * FROM public.user" +
" INNER JOIN public.company" +
" ON (public.user.company_id = public.company.id)" +
" INNER JOIN public.truck" +
" ON (public.user.id = public.truck.user_id)" +
" INNER JOIN public.job" +
" ON (public.truck.id = public.job.truck_id)" +
" WHERE public.company.id = :companyId" +
" ORDER BY :columnNumber USING < LIMIT :pageSize OFFSET :offset")
List<Object[]> findJobsDESC(@Param("companyId") Long companyId, @Param("pageSize") int pageSize, @Param("columnNumber") int columnNumber, @Param("offset") int offset);
companyId = 1
,pageSize = 10
,columnNumber = 1
和offset = 0
。
当我运行这两个函数时,我得到相同的结果,它没有被命令。我尝试过使用ASC
和DESC
但没有成功。我也尝试使用columnName
代替columnNumber
,但它仍然无法发挥作用。
我做错了什么?