双向关联查询问题:如何使用 GORM 查询所属类的所有者?
假设您有两个域类:作者和书籍。作者可以拥有多本书,但一本书只能有一个作者。
class Author {
static hasMany = [Book]
}
class Book {
static belongsTo = Author
}
怎么查询书的作者?
这似乎不起作用:
def book = Book.get(1)
book.author
Say you have two domain classes: Authors and Books. Authors can have many Books, but a Book can only have one Author.
class Author {
static hasMany = [Book]
}
class Book {
static belongsTo = Author
}
How do you query is book's author?
This doesn't seem to work:
def book = Book.get(1)
book.author
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您的代码更改为:
那么这应该有效:
change your code to:
then this should work: