使用级联 all-delete-orphan 进行 removeFrom 不起作用
Grails 1.3.7 和 MySQL 5.5
要么我一定是在做一些脑死亡的事情,要么这与 Grails 问题有关 http://jira.grails.org/browse/GRAILS-5804 和 http://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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你放错了约束,它应该是这样的:
NOT
That导致了问题。有关此 GORM 陷阱的更多信息可以在此处找到(删除子项)。
I think you misplace the constraint, it should be like this:
NOT
That causes the problem. More information about this GORM gotchas can be found here(Deleting children).