在Grails中解释属于 [英] Explain belongsTo in Grails
问题描述
从 Grails belongsTo 文档,什么是使用
class Book {
static belongsTo = Author
}
在Author上执行CRUD操作时,级联操作对Book的影响是什么?
编辑:
感谢您的回复,可能是我没有正确指定我的问题。我想知道 $ b
静态belongsTo [作者:作者]
vs
static belongsTo = Author
belongsTo
如果您需要参考返回到拥有的对象,则会很有帮助。在这种情况下, Author
可能有许多 Book
s。但是,也许你正在使用书籍对象,并希望提及该书籍实例的 Author
。这是获取它的好方法。
就CRUD而言,删除或更新书籍对作者 code>,但删除
Author
会删除 Book
。如果您不添加 belongsTo
,那么不会有层叠保存/更新/删除,您必须手动完成。
示例:
def a = new作者(名称:'JK Rawling' )
a.addToBooks(新书(标题:'哈利波特1'))
a.addToBooks(新书(标题:'哈利波特2'))
a.save() //保存作者和书籍实例
a.delete()//作者和书籍都被删除
编辑:
OP更新了他们的问题,我真的不确定答案会是什么。希望Burt Beckwith很快会出现!好问题,OP。
From the Grails belongsTo documentation, what is the use of
class Book {
static belongsTo = Author
}
What is the impact of cascading operations on Book, when CRUD operations performed on Author?
EDIT:
Thanks for your responses, may be i didn't specify my question correctly. I would like to know the difference between
static belongsTo [author:Author]
vs
static belongsTo = Author
belongsTo
is helpful if you need a reference back to the owning object. In this case it is likely that an Author
has many Book
s. But maybe you're using a book object and want to mention that book instance's Author
. That is a good way to get it.
As far as CRUD goes, deleting or updating the book will not do anything to the Author
, but deleting the Author
will delete the Book
. If you don't add belongsTo
then there will be no cascading saves/updates/deletes, you will have to do it manually.
Example:
def a = new Author(name: 'J.K. Rawling')
a.addToBooks(new Book(title: 'Harry Potter 1'))
a.addToBooks(new Book(title: 'Harry Potter 2'))
a.save() // Saves author and book instances
a.delete() // Author and both books are deleted
Edit:
The OP updated their question, and I'm honestly not sure what the answer would be. Hopefully Burt Beckwith will show up soon! Good question, OP.
这篇关于在Grails中解释属于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!