Closed domiko closed 9 years ago
I've found this post: http://blog.tremend.ro/2008/06/10/how-to-order-by-a-custom-sql-formulaexpression-when-using-hibernate-criteria-api/ and I've made a very quick test with that class and this code:
// Service method
List<TestMapJsonb> orderByJson() {
TestMapJsonb.withCriteria {
order sqlFormula("(data->'name') desc")
}
}
and this test:
void 'Order by a json property'() {
setup:
new TestMapJsonb(data: [name: 'Iván', lastName: 'López']).save(flush: true)
new TestMapJsonb(data: [name: 'Alonso', lastName: 'Torres']).save(flush: true)
new TestMapJsonb(data: [name: 'Ernesto', lastName: 'Pérez']).save(flush: true)
when:
def result = pgJsonTestSearchService.orderByJson()
then:
result != null
result.data.name == ['Iván', 'Ernesto', 'Alonso']
}
And it works :smile:
It's a little bit hacky because you need to write the correct order expression and it's just appended to the criteria. Does it sounds good to you?
BTW @domiko you can see all the changes here: https://github.com/kaleidos/grails-postgresql-extensions/compare/master...order_by
Hey @lmivan, Hacky or not, this is exactly what I needed :-) . Works like a charm.
Thanks for your assistance and very quick response.
Great. I'm going to try to fix another issue and then I'll release a new version with this new sorting and another "sort byRandom".
Included in version 4.6.0
Thanks @lmivan
:+1:
Given the following schema
and the content of the stats field that can look like:
How would you do the following through GORM (HQL/Criterias/dynamic_finders) with the grails-postgresql-extensions plugin?
Thanks, Dominique.