Grails / Hibernate:如何通过isnull(property)来最后获取NULL值? [英] Grails/Hibernate: how to order by isnull(property) to get NULLs last?
问题描述
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屋!