Spring Data 使用排序不在最后 [英] Spring Data using sort not at the end
问题描述
我可以在最后的表达式中给出排序参数吗?因为我需要在 over() 函数中使用 sort 参数.
Can I give sort parameter not at the end expresion? Because i need use sort parameter inside over() function.
示例:
@Query(value = "...?2...over(?4)...?1...?3")
myMethod(int a,int b,int c,Sort sort);
或用于内部排序的表达式: ,--#sort\n , #{#sort} ..
or expression for sort inside over: ,--#sort\n , #{#sort} ..
我仍然得到:
java.lang.IllegalArgumentException: 方法 public abstract java.lang.Integer ...name of method.. (int,int,int,org.springframework.data.domain.Sort) 的查询验证失败!>
java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.lang.Integer ...name of method.. (int,int,int,org.springframework.data.domain.Sort)!
推荐答案
Sort
类型的参数得到 Spring Data JPA 的特殊处理.
Parameters of type Sort
get special treatment by Spring Data JPA.
即使情况并非如此,您尝试实现的目标也无法通过简单的绑定参数实现,因为 SQL 允许在这些地方绑定参数.
Even if that wouldn't be the case what you try to achieve can't be accomplished with a simple bind parameter, because bind parameters allowed in these places by SQL.
根据您想要实现的目标,您可能可以执行以下操作:
Depending on what you want to achieve you might be able to do something like the following:
@Query("select .... over(... order by CASE ?1 WHEN "first THEN first
WHEN 'last' THEN last
END")
public Something method(String columnNameForSorting)
假设您有列 first
和 last
要在它们之间切换以进行排序.
assuming you have the columns first
and last
you want to switch between for sorting.
Oracle DECODE 的标准 SQL 替代方案,了解有关 CASE 表达式的更多信息
Standard SQL alternative to Oracle DECODE for more information about the CASE expression
这篇关于Spring Data 使用排序不在最后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!