【问题标题】:Grails gorm catch foreign key constraint errorGrails gorm 捕获外键约束错误
【发布时间】:2014-05-22 05:48:33
【问题描述】:

在 Grails 中我想在出现外键约束时捕获异常,这是我的代码

    try {
        instance.delete(flush:true)
        flash.message = message(code : "default.deleted.message", args : [instance])
        flash.level = "info"
    } catch(org.springframework.dao.DataIntegrityViolationException | Exception e) {
        flash.message =  message(code : "default.not.deleted.message", args :  [instance])
        flash.level = "danger"
    }

问题是当有外键约束时,它永远不会进入 catch 块。 知道我应该添加什么例外吗?

谢谢,

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

    我正在使用grails 2.3.5,以下代码适用于我

    try {
        user.delete(flush: true)
        render "OK"
    } catch (org.springframework.dao.DataIntegrityViolationException e) {
        render "DataIntegrityViolationException"
    } catch (Throwable v) {
        render "Throwable"
    }
    

    试试这个..,.

    【讨论】:

      【解决方案2】:

      我认为这是因为MySQLIntegrityConstraintViolationException 被抛出updateinsert 而不是delete

      Exception thrown when an attempt to insert or update data results in violation of an
       integrity constraint. Note that this is not purely a relational concept; unique primary
       keys are required by most database types.
      

      试试com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException

      编辑

      您使用的是哪个版本的 Grails

      我发现了这个问题GRAILS-9357

      【讨论】:

      • 是的..这是 jirra 中的问题,您将永远没有机会缓存​​此异常,它是一个错误..
      猜你喜欢
      • 2021-07-02
      • 1970-01-01
      • 2021-07-11
      • 2011-06-12
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多