我在我的一个方法中做了类似的事情:
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
答案 0 :(得分:1)
变通方法会导致不必要的数据库调用,保证不会返回任何内容。请改用:
def shades = shadeIds ? MyShop.getAll(shadeIds).shades : []