编辑:得到-1,请您解释为什么?我搜索了重复项,但没有找到任何重复项。

发布我刚刚遇到的问题的问题解答:

class Pineapple {
    def pineappleService

    Supplier supplier;

    def beforeInsert() {
        pineappleService.beforeInsert(this);
    }
}

class PineappleService {
    def beforeInsert(Pineapple pineapple) {
         Pineapple.withNewSession {
             // some logic
             pineapple.supplier.save();
         }
    }
}

异常(exception):

最佳答案

诀窍是将闭包移动到域类:

class Pineapple {
    def pineappleService

    Supplier supplier;

    def beforeInsert() {
        Pineapple.withNewSession {
            pineappleService.beforeInsert(this);
        }
    }
}

class PineappleService {
    def beforeInsert(Pineapple pineapple) {
         // some logic
         pineapple.supplier.save();
    }
}

说明文件:
  • GORM: Events and Auto Timestamping


  • https://grails.github.io/grails-doc/latest/ref/Domain%20Classes/withNewSession.html
  • 07-25 20:29