【问题标题】:Grails Duplicate Exception handlingGrails 重复异常处理
【发布时间】:2010-04-20 04:35:11
【问题描述】:

如何在 Grails 中捕获重复的键异常。尝试为唯一列约束保存现有整数时,在保存/更新记录时会生成错误。

也用过

try{
    object.save(flush:true)
}
catch(org.springframework.dao.DataIntegrityViolationException e){
    println e.message
}
catch(org.hibernate.exception.ConstraintViolationException ex){
    println e.message
}
catch(Exception e){
    println e.message
}

但无法捕捉到这个问题。

23:41:13,265 错误 [JDBCExceptionReporter:101] 重复条目“1” 关键 2
23:41:13,281 错误 [AbstractFlushingEventListener:324] 无法将数据库
状态与会话同步 org.hibernate.exception.ConstraintViolationException:不能 执行 JDBC 批量更新 org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94) 在 org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) 在 org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275) 在 org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266) 在 org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168) 在 org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) 在 org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50) 在 org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)

你能分享一下解决这个问题的方法吗?

【问题讨论】:

    标签: grails


    【解决方案1】:

    您正在尝试使用已存在的 id 保存记录。

    • 如果 id 是自动生成的,请不要手动设置
    • 如果 id 不是自动生成的,请将其设置为其他值,例如 max(id) + 1

    【讨论】:

    • 不,id 不是自动生成的,并且 column 是唯一键。它是一个要输入的文本字段,当用户输入现有整数时,它将无法保存。这可以正常工作,但在控制台中我收到了提到的错误。
    【解决方案2】:

    当然不应该因为违反约束而抛出异常,而是 object.save() 应该返回 null?即

    if(object.save() == null) {
        // save failed
    } else {
        // save succeeded
    }
    

    【讨论】:

      【解决方案3】:

      如果您通过 Grails 约束定义唯一性,则必须查找 ValidationException。当 object.validate() 失败时抛出此错误;验证在任何 object.save() 之前完成。

      try {
        object.save(failOnError: true)
      }
      catch(ValidationException ve) {
        // Do something ...
      }
      

      但请记住:任何违反约束的行为,对于任何成员变量都可能导致 ValidationException ...因此您必须区分自己。

      编辑:

      这适用,如果您使用 Grails 1.2 failOnError 功能...

      【讨论】:

      • 是的,但我使用的是 grails-1.1.2
      • 好的,现在有两种可能:要么尝试常见的 Grails 验证过程,例如 if(obj.validate()) { obj.save() } else { obj.errors.each { // doh ,有错误... } }(如 Ali G 提到的)或更简单、更清洁的“grails 升级”;)
      【解决方案4】:

      我正在寻找同样的问题,所以可能不是一个完整的答案,但您可以做的是强制验证并查看错误,确定案例并执行您想要的操作:

      if(instance.validate()) {
          //everything ok 
      } else {
          instance.errors.each {
                  //identify the case and place actions
          }
      }
      

      还要注意错误是:className.propertyName.unique

      【讨论】:

        【解决方案5】:

        可能它应该工作:

        import org.springframework.dao.DuplicateKeyException
        
        try {
            domainInstance.save(flush: true)
        } catch(DuplicateKeyException e) {
            // ...
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-11-09
          • 2014-01-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-12
          • 1970-01-01
          相关资源
          最近更新 更多