Spring Data using sort not at the end

为君一笑 提交于 2019-12-25 17:45:13

问题


Can I give sort parameter not at the end expresion? Because i need use sort parameter inside over() function.

example:

@Query(value = "...?2...over(?4)...?1...?3")
myMethod(int a,int b,int c,Sort sort);

or expression for sort inside over: ,--#sort\n , #{#sort} ..

I still get :

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)!


回答1:


Parameters of type Sort get special treatment by Spring Data JPA.

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)

assuming you have the columns first and last you want to switch between for sorting.

Standard SQL alternative to Oracle DECODE for more information about the CASE expression



来源:https://stackoverflow.com/questions/46861276/spring-data-using-sort-not-at-the-end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!