Sort based on column in child table using GORM?

我们两清 提交于 2019-12-10 16:48:21

问题


I have a table called employee and child table address.

Now I want to get a list of employees sort by address1 in address table using GORM.

Employee.findAllByName(name, [max: maxRecords, offset: 100,sort: Address.address1, order: desc])

the above statement is not working, any suggestions would be appreciated.

Thanks


回答1:


Try using a criteria query like so...

def c = Employee.createCriteria()
def results = c.list (max: maxRecords, offset: 100) {
    eq("name", name)
    address {
        order("addres1", "desc")
    }

}

This works for me!

Another option is to add a default sort order like so...

class Address{
    …
    static mapping = {
        sort address1:"desc"
    }
}

However, I always prefer to do things as an 'as-needed' basis rather than define that sorting be done every time even when it may not be needed. U pick. Enjoy!



来源:https://stackoverflow.com/questions/10618345/sort-based-on-column-in-child-table-using-gorm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!