Grails/Hibernate: how to order by isnull(property) to get NULLs last?

江枫思渺然 提交于 2019-11-30 20:27:34

In hibernate you can try with this below code :

Criteria c = ...;

c.addOrder(Order.asc("name").nulls(NullPrecedence.LAST));

if you want to order and HibernateCriteriaBuilder to set NullPrecedence in GORM the AbstractHibernateCriteriaBuilder provies you with the method order() that you can set a org.hibernate.criterion.Order like regular Hibernate

Example

     People.createCriteria().list (){
          order(org.hibernate.criterion.Order.asc('name')
                . nulls(org.hibernate.NullPrecedence.LAST)
           )
     }

I'm afraid it's not even in Hibernate yet: there's an open bug for this.

Though, one could use NativeSQLOrder from that bug comments and try to inject a proper function into HibernateCriteriaBuilder. You only need to add a sqlOrder method to HibernateCriteriaBuilder class, doing approximately same as HibernateCriteriaBuilder.order().

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