How do I sort by a property on a nullable association in Grails?

被刻印的时光 ゝ 提交于 2019-12-03 20:18:37

You need to specify LEFT_JOIN in the query, try this:

import org.hibernate.criterion.CriteriaSpecification

...

def list = Car.createCriteria().list ([max:params.max?:10,  offset: params.offset?:0 ]){

        if (params.sort == 'engine.name') {
            createAlias("engine","e", CriteriaSpecification.LEFT_JOIN)
            order( "e.name",params.order)

        } else {                
            order(params.sort, params.order)
        }
    }

Remember to put engine.name as the property to order by in your list.gsp

<g:sortableColumn property="engine.name" title="Engine Name" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!