简体   繁体   English

GORM domain.delete(flush: true) 不触发 HibernateException 或其他反馈?

[英]GORM domain.delete(flush: true) not firing HibernateException nor other feedback?

Given:鉴于:

Foo () {
  Bar bar

  static contraints = {
      bar (nullable: false)
  }
}

and a Service to manage Bar types, where a method deletes:和一个用于管理 Bar 类型的服务,其中一个方法删除:

line 105: BarService {
line 106:     public BarCommand deleteBar(long Id) {    
line 107:         Bar b = Bar.get(Id)
line 108:         if (!b.delete(flush: true)) {
line 109:             //handle error here

If there is an existing Foo, "f" that already has a Bar of id 1 (for exmaple) associated with it, then line 108 should fail with some error explaining "b cannot be deleted, it is assigned to Foo id: 1" however, all that happens is line 108 returns false on truthiness and drops into the error handling regardless of whether delete was successful or not.如果有一个现有的 Foo,“f”已经有一个 id 1(例如)的 Bar 与之关联,那么第 108 行应该会失败并出现一些错误,解释“b 不能被删除,它被分配给 Foo id: 1”然而,所发生的只是第 108 行在真实性上返回 false 并进入错误处理,无论删除是否成功。

How do we know when we delete a domain whether the delete succeeded or failed?我们如何知道删除域时删除成功还是失败? the doco does not describe this - Hibernate reports that a HibernateException should fire, but no exceptions are fired in any Unit test scenario... doco 没有描述这一点 - Hibernate 报告 HibernateException 应该触发,但在任何单元测试场景中都不会触发任何异常......

( http://docs.grails.org/latest/ref/Domain%20Classes/delete.html ) ( http://docs.grails.org/latest/ref/Domain%20Classes/delete.html )

I realize domain.delete() would not return anything as the delete is not communicated to the DB until a flush event, so I thought explicitly calling flush: true would address this, but it does not appear to.我意识到 domain.delete() 不会返回任何内容,因为在刷新事件之前删除不会传达给数据库,所以我认为明确调用 flush: true 可以解决这个问题,但它似乎没有。

Personally I dislike the inefficiency of retrieving an object from the DB for the sole purpose of deleting it, so instead of this: 我个人不喜欢仅出于删除目的而从数据库中检索对象的效率低下,因此,请执行以下操作:

Bar b = Bar.get(Id)
if (!b.delete(flush: true)) {
  // handle error here
}

I would use HQL to perform the delete because this avoids the need to retrieve it first (the load() method is an alternative solution). 我将使用HQL执行删除,因为这避免了首先检索它的需要( load()方法是替代解决方案)。

Integer rowsDeleted = Bar.executeUpdate('delete Bar where id = ?', [Id])
if (!rowsDeleted) {
  // handle error here
}

A further benefit of this approach is that executeUpdate returns the number of rows updated/deleted, so you can check whether the operation was successful. 这种方法的另一个好处是executeUpdate返回已更新/删除的行数,因此您可以检查操作是否成功。

i have used with transaction commit changes in database and its working great read this link for more information.我已经使用了数据库中的事务提交更改,它的工作非常好,请阅读此链接以获取更多信息。 https://github.com/grails/grails-core/issues/10444 https://github.com/grails/grails-core/issues/10444

def delete(long id) { def 删除(长 ID){

    Document FSDocumentInstance = Document.get(id)
    Document.withTransaction {
        if (FSDocumentInstance == null) {
            return
        }
        File file = new File(FSDocumentInstance.filePath)
        File test = new File(FSDocumentInstance.fullPath)
        test.delete()
        file.delete()
        FSDocumentInstance.delete()
        flash.message = "File deleted"
    }
        redirect(action: 'index')
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM