2009-07-24 218 views
0

與圖書和作者域類的國有成員如下:搜索多對一對多的關係

class Book { 
    static belongsTo = Author 
    static hasMany = [authors:Author] 
    String title 
} 

class Author { 
    static hasMany = [books:Book] 
    String name 
} 

我怎麼會找到這本書由具有標題「的Grails」的作者?

我試過,但它沒有工作(法無簽名:)org.hibernate.collection.PersistentSet.findByTitle(適用於arguemnt類型:(java.lang.String中)值:[Grails的]

Author author = Author.get(1) 
def book = author.books.findByTitle("Grails") 

回答

1

您可以通過舉例如下搜索。

 
Author author = Author.get(1) def b = Book.find(new Book(title:'grails', author:author)) 

看到this link關於如何做querys信息。

+0

這是我需要有小幅調整。我有一個作家實例,所以代碼變成: 作者author = Author.get(1) def b = Book.find(new Book(title:'grails',author:author)) 謝謝! – byamabe 2009-07-24 16:47:14