Grails / Hibernate:如何通过isnull(property)来最后获取NULL值? [英] Grails/Hibernate: how to order by isnull(property) to get NULLs last?

查看:303
本文介绍了Grails / Hibernate:如何通过isnull(property)来最后获取NULL值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,当按字段升序时,首先获得NULL值,然后获得更有趣的值。通常,你最后需要NULL值。在MySQL中,你可以这样做:

SELECT * FROM people ORDER BY ISNULL(name),name; 然而,我使用Grails和Hibernate标准,并且我绝对不知道如何在那里做到这一点。这是否以任何方式得到支持?有一种方法可以通过自定义SQL表达式进行排序吗?我不想重写我的所有条件,只是为了使它正确地排序。

解决方案

如果您想要订购和 HibernateCriteriaBuilder GORM 中设置 NullPrecedence AbstractHibernateCriteriaBuilder a href =http://grails.github.io/grails-data-mapping/latest/api/org/grails/orm/hibernate/query/AbstractHibernateCriteriaBuilder.html#order(org.hibernate.criterion.Order) =nofollow> order(),您可以像常规Hibernate一样设置 org.hibernate.criterion.Order

示例

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

}


Normally when ordering ascending by a field, you get the NULL values first, and then the more interesting values. Often, you want NULL values last. In MySQL, you can do this with:

SELECT * FROM people ORDER BY ISNULL(name), name;

However, I'm using Grails with Hibernate criteria, and I have absolutely no idea how to do this there. Is this even supported in any way? Is there some way to order by a custom SQL expression? I'd hate to rewrite all my criteria to plain SQL just to get it to sort correctly.

解决方案

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

这篇关于Grails / Hibernate:如何通过isnull(property)来最后获取NULL值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆