How to define null-handling in Spring Data JPA sorts?

点点圈 提交于 2019-12-10 10:15:43

问题


I need my data to be ordered by null and not null only. Like below:

Column A
null 
null 
null
8 
10
5

Meaning once all the null values are on top, the rest of the values should not be sorted at all and come as is. Any idea how this can be done using jpa query/ native query.


回答1:


Spring Data's Sort object allows you to define:

Sort sort = new Sort(new Order(Direction.ASC, "property").nullsLast());



回答2:


Use JPQL and you will be happy.

SELET f FROM Foo f ORDER BY f.name NULLS LAST


来源:https://stackoverflow.com/questions/29401227/how-to-define-null-handling-in-spring-data-jpa-sorts

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