【问题标题】:GORM - all-delete-orphan not workingGORM - all-delete-orphan 不起作用
【发布时间】:2012-11-03 21:46:33
【问题描述】:

我在 mygrails 项目中有 2 个类,名为 Profile 和 License 以下是课程中重要内容的大纲

class Profile {

    Set<Licence> licenceKeys

    static hasMany = [licenceKeys:Licence]

   static constraints = {
        licenceKeys nullable:true
    }
    static mapping = {
        licenceKeys cascade: 'all-delete-orphan'
    }
}

class Licence {

    Profile profile
    String licenceKey

    static belongsTo = [profile:Profile]

    static constraints = {
        profile nullable:false
        licenceKey blank:true, nullable:true, unique:true
    }   
}

当我在我的一个控制器中运行以下代码时

    if (!profile.licenceKeys.clear()) {
        log.error ("Error occured removing licence keys from the database");
        userProfile.errors.each { err -> println err; }
    }  else {
        log.info("Successfully remove licence keys from the database");
    }

我在控制台中收到以下错误消息,并且许可证密钥保留在数据库中 org.springframework.validation.BeanPropertyBindingResult: 0 个错误 密钥已从 Profile 类中设置的 licenceKeys 中删除,但仍保留在数据库中

我有 2 个问题 是否有可能为我收到的错误消息获得更好的调试输出 我是否遗漏了确保从数据库中删除 licekey 的任何内容?

谢谢

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

    clear() 方法实际上不会执行删除clear() 之后要保存的数据库的工作。试试……

    profile.licenceKeys.clear();
    if (!profile.save(flash:true)) {
        log.error ("Error occured removing licence keys from the database");
        userProfile.errors.each { err -> println err; }
    }  else {
        log.info("Successfully remove licence keys from the database");
    }
    

    对于您的第二个问题,您需要从保存失败的域对象中获取错误......

     if (!profile.save(flash:true)) {
        log.error ("Error occured removing licence keys from the database");
        profile.errors.each { err -> println err; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-22
      相关资源
      最近更新 更多