简体   繁体   English

Grails一对多删除而无级联

[英]Grails one-to-many delete without cascade

I'm trying to create a bi-directional many-to-one relationship in Grails with NON-cascading deletes in both directions. 我正在尝试在Grails中创建双向多对一关系,并在两个方向上进行非级联删除。 My domain looks like: 我的域名如下:

class Asset {
  static hasMany = [ hosts: Host ]

  static mapping = {
      hosts cascade: 'save-update'
  }
}

class Host {
  Asset asset
}

This works fine when deleting a Host (the Asset remains), but when I try to delete the Asset, I get an integrity constraint error from the DB. 删除主机(资产仍保留)时,此方法工作正常,但是当我尝试删除资产时,从数据库中收到完整性约束错误。 I've tried changing the Host class definition to use a belongsTo, but can't get this to work. 我尝试过更改Host类的定义以使用belongsTo,但是无法使其正常工作。 Any help would be great! 任何帮助将是巨大的! Thanks! 谢谢!

You have parent (Asset) and children (Host) classes with the following rules/properties: 您具有具有以下规则/属性的父类(资产)和子类(主机):

  1. Parent has children 父母有孩子
  2. Children can't exists without parent (bi-directional) 孩子没有父母就不能存在(双向)
  3. Parent can't remove children 父母不能带走孩子

Your dilemma is you want to be able to remove Parent - but you would be breaking rule #2. 您的难题是您希望能够删除父级-但您将违反规则2。 Even with a mapping table, I don't think you can do what you're asking. 即使有映射表,我也不认为您可以完成您所要求的。 I've never had this use case, and without re-designing too much of your current model, my guess is that you have to: 我从来没有用过这个用例,并且在不重新设计当前模型的情况下,我想您必须:

  1. as @JamesKleeh pointed out - allow nullable on Host.asset - basically Children CAN exists without parent 正如@JamesKleeh所指出的-在Host.asset上允许为空-基本上,子代可以不存在父代而存在
  2. or allow for parent to remove children (ie, cascade delete) 或允许父母删除孩子(即级联删除)

这是一个多对多关系,在GORM中这种关系实际上可以按您想要的方式工作:您可以将hosts添加到Asset所有者,并安全删除Asset而不影响hosts

I ended up finding a solution by writing my own delete() action for the Asset controller that deletes all the references to the Asset from all hosts before deleting the Asset itself: 我最终通过为资产控制器编写自己的delete()操作找到了解决方案,该操作在删除资产本身之前从所有主机中删除了对资产的所有引用:

def delete() {
    def assetInstance = Asset.get(params.id)        
    assetInstance.hosts.each { theHost ->
        theHost.asset = null
        theHost.save()
    }
    if(!assetInstance.hasErrors() && assetInstance.delete()) {
        redirect(action: "list")
    }
    else {
        redirect(url: "/asset/show?id=${assetInstance.id}")
    }
}

This eliminates the error and also prevents children (Hosts) from being deleted when the parent (Asset) is deleted. 这样可以消除错误,并且还可以防止在删除父项(资产)时删除子项(主机)。

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

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