Reputation: 137
i am facing a problem sorting a list that has null values !
What i need is that the null values appear at the last of the list when ordering ! as it seems now Nulls are on the top of my list when ordering it !
I couldn't use NullPrecedence as the hibernate version is 3.6.x
sample of my code !
SomeClass.createCriteria().list {
eq('sth', sth)
if (sort == 'someValue') {
createAlias("nestedClass1", "nc1", CriteriaSpecification.LEFT_JOIN)
createAlias("nc1.nestedClass2", "nc2", CriteriaSpecification.LEFT_JOIN)
createAlias("nc2.nestedClass3", "nc3", CriteriaSpecification.LEFT_JOIN)
order("nc3.name", sortOrder)
}
}
Upvotes: 0
Views: 811
Reputation: 911
you can add new field in your domain class with mapping to formula that checks if it's null, and then sort by this extra field:
class SomeClass {
String name
boolean nameNotNull
static mapping = {
nameNotNull formula: 'name is not null'
}
}
SomeClass.createCriteria().list() {
order ('nameNotNull', 'desc')
order ('name', 'asc')
}
Upvotes: 2