在GORM中从查询中找不到任何内容时的解决方法

时间:2013-03-12 15:50:11

标签: grails gorm

我在我的一个方法中做了类似的事情:

    def colorId = Store.findByName(color).id
    def shadeIds = Shades.findAllByColor(colorId).shadeId
    println "IDS" + shadeIds //sometimes this is empty ([])
    MyShop.getAll(shadeIds).shades
shadeIds为空[]时从上面的代码

我在执行MyShop.getAll(shadeIds).shades时遇到SQL错误。是否有解决方法?

我的临时解决方法是:

    def colorId = Store.findByName(color).id
    def shadeIds = Shades.findAllByColor(colorId).shadeId
    println "IDS" + shadeIds //sometimes this is empty ([])
    if (shadeIds.size() == 0)
      shadeIds << -1
    MyShop.getAll(shadeIds).shades

1 个答案:

答案 0 :(得分:1)

变通方法会导致不必要的数据库调用,保证不会返回任何内容。请改用:

def shades = shadeIds ? MyShop.getAll(shadeIds).shades : []