使用级联 all-delete-orphan 进行 removeFrom 不起作用

发布于 2024-11-02 04:32:36 字数 1612 浏览 5 评论 0原文

Grails 1.3.7 和 MySQL 5.5

要么我一定是在做一些脑死亡的事情,要么这与 Grails 问题有关 http://jira.grails.org/browse/GRAILS-5804http://jira.grails.org/browse/GRAILS-4121 和类似的。 我有:

class Author {
    String name
    static hasMany = [books: Book]
    static constraints = {
        books cascade: 'all-delete-orphan'
    }
    String toString() {
        return name
    }
}

class Book {
    String title
    static belongsTo = [author: Author]
    static constraints = {
    }
    String toString() {
        return title
    }
}

Bootstrap:
def a = new Author(name: 'Author0')
a.save(flush: true, failOnError: true)      
def b = new Book(title: 'Book0')
a.addToBooks(b).save(flush: true, failOnError: true)

class AuthorController {
    def index = {
        println("Controller code: " + "Old books by author: " + Author.get(1).books)
        def author = Author.findByName("Author0")
        def oldBooks = []
        oldBooks += author.books  // to avoid ConcurrentModificationException
        oldBooks.each {
            author.removeFromBooks(it)
        }
        author.save(flush: true, failOnError: true)
        println("Controller code: " + "New books by author: " + Author.get(1).books)
            render("All done!")
        }
    }

但是当我导航到 localhost:8080/foo/author/index 时,我在 save() 期间得到一个“非空属性引用空值或瞬态值:foo.Book.author”

我真的不知道 GORM -internals 或 Hibernate(代理实例与非代理实例),我尝试了使用“get”而不是“find”的所有组合,但无法使其正常工作。有人可以解释这应该如何工作吗?

Grails 1.3.7 with MySQL 5.5

Either I must be doing something brain-dead, or this is related to Grails issues http://jira.grails.org/browse/GRAILS-5804 and http://jira.grails.org/browse/GRAILS-4121 and similar.
I have:

class Author {
    String name
    static hasMany = [books: Book]
    static constraints = {
        books cascade: 'all-delete-orphan'
    }
    String toString() {
        return name
    }
}

class Book {
    String title
    static belongsTo = [author: Author]
    static constraints = {
    }
    String toString() {
        return title
    }
}

Bootstrap:
def a = new Author(name: 'Author0')
a.save(flush: true, failOnError: true)      
def b = new Book(title: 'Book0')
a.addToBooks(b).save(flush: true, failOnError: true)

class AuthorController {
    def index = {
        println("Controller code: " + "Old books by author: " + Author.get(1).books)
        def author = Author.findByName("Author0")
        def oldBooks = []
        oldBooks += author.books  // to avoid ConcurrentModificationException
        oldBooks.each {
            author.removeFromBooks(it)
        }
        author.save(flush: true, failOnError: true)
        println("Controller code: " + "New books by author: " + Author.get(1).books)
            render("All done!")
        }
    }

But when I navigate to localhost:8080/foo/author/index I get a 'not-null property references a null or transient value: foo.Book.author' during the save()

I don't really know GORM-internals or Hibernate (proxied vs unproxied instances), I tried all combinations of using 'get' instead of 'find' but can't get this to work. Can someone explain how this is supposed to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱她像谁 2024-11-09 04:32:36

我认为你放错了约束,它应该是这样的:

static mapping = {
    books cascade: "all-delete-orphan"
}

NOT

static constraints = {
    books cascade: 'all-delete-orphan'
}

That导致了问题。有关此 GORM 陷阱的更多信息可以在此处找到(删除子项)。

I think you misplace the constraint, it should be like this:

static mapping = {
    books cascade: "all-delete-orphan"
}

NOT

static constraints = {
    books cascade: 'all-delete-orphan'
}

That causes the problem. More information about this GORM gotchas can be found here(Deleting children).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文