如何防止Grails在删除父项时不删除子项? [英] How to prevent from Grails not to delete child while deleting parent?

查看:165
本文介绍了如何防止Grails在删除父项时不删除子项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有作者和书籍之间的一对多关系,一位作者有很多书籍。



我有这样的域名类

Author.groovy

  class作者{

字符串authorName;
String authorAddress;
字符串authorCNIC;

static hasMany = [books:Book]

static constraints = {

books(nullable:true)
}
}

Book.groovy

  class Book {

String bookName;
String Isbn;

static belongsTo = [author:Author]

static constraints = {

author(nullable:true)
}



}

现在当我调用这个函数

  def deleteauthor()
{
def id = params.id
作者author = Author .findById(ID);
author.delete(flush:true)
render作者已删除
}

删除作者及其所有儿童书..我不希望这种行为,我希望它不能删除作者和书籍,并显示消息不能删除作者,先删除书籍...请告诉我如何在您的作者模型中,您应该添加 cascade:'none'

解决方案

  class Author {

String authorName;
String authorAddress;
字符串authorCNIC;

static hasMany = [books:Book]

static mapping = {
cascade:'none'
}
}

这样可以防止删除书籍,并将 author_id 设置为



更新:



似乎设置 cascade'none'也会禁用级联保存。
因此,一种方法是在每个添加的书籍实例上手动调用 save





您可以将级联设置为仅在 save-update 上工作,但当作者删除时存在违反约束异常,因为在Hibernate上,没有办法在delete set null 上设置。我们可以在 beforeDelete 手动执行。

  class Author {

字符串名称;

static hasMany = [books:Book]

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

def beforeDelete(){
Book.withNewSession {
Book.findAllByAuthor(this).each {
it.author = null
it.save(flush:true)
}
}
}
}

它似乎工作,虽然它不是很漂亮。


i have one to many relationship between author and books, one author has many books..

i have domain class like this

Author.groovy

class Author {

    String authorName;
    String authorAddress;
    String authorCNIC;

    static hasMany = [books:Book]

    static constraints = {

        books(nullable: true)
    }
}

Book.groovy

class Book {

    String bookName;
    String Isbn;

    static belongsTo = [author:Author]

    static constraints = {

        author(nullable: true)
    }



}

now when i call this function

def deleteauthor()
    {
        def id=params.id
        Author author = Author.findById(id);
        author.delete(flush: true)
        render "Author Deleted"
    }

it deletes author and all of its child books..i do not want this behaviour, i want that it cannot delete author and books and display message cannot delete author, first delete books...please tell me how to do this ?

解决方案

In your Author model you should add cascade: 'none' to constraints:

class Author {

    String authorName;
    String authorAddress;
    String authorCNIC;

    static hasMany = [books:Book]

    static mapping = {
        cascade: 'none'
    }
}

This would prevent deleting books and would just set author_id to null.

UPDATE:

It seems that setting cascade 'none' would also disable cascading saves. So one option is to manually call save on every added book instance.

Other thing I figured:

You can set cascade to only work on save-update but then when Author is delete there is constraint violation exception, because there is not way to set on delete set null on Hibernate. We could do it manually on beforeDelete.

class Author {

    String name;

    static hasMany = [books:Book]

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

    def beforeDelete() {
        Book.withNewSession {
            Book.findAllByAuthor(this).each {
              it.author = null
              it.save(flush: true)
            }
        }
   }
}

It seems to be working, although it's not very pretty.

这篇关于如何防止Grails在删除父项时不删除子项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆