2013-05-11 18 views
0

我有個域HibernateCriteriaBuilder的Grails

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

Book{ 
    String title 
    String publisher 
    belongsTo = [author:Author] 
} 

我可以有唯一的輸入是AUTHORID。使用此值如何獲取域名記錄使用HibernetCriteriaBuilder eq(propertyName, propertyValue)

謝謝!

回答

2

在回答問題之前,很少有檢查點: -
1. id默認情況下,id綁定到域類,您可能不需要AuthorId
2. Grails按照慣例。你想用authorId而不是AuthorId來避免錯誤的狀態。

如果你已經通過了this page那麼到現在爲止你會對grails中的criteria有基本的想法。除此之外,criteria也可用於associations

在你的使用情況,您可以做這樣的事情: - 如果你想從Author

def books = Author.createCriteria().get{ 
     eq('AuthorId', authorId) 
}.books 

得到所有的Books但是,這是一個漫長的過程,這可以很容易的確可以做到

def books = Author.findByAuthorId(authorId)?.books
爲什麼我們需要一個標準?

Grails的方法要簡單得多: def books = Author.get(id)?.books

在標準變爲:

def books = Author.createCriteria().get{ 
      idEq("abc123") //'abc123' is your authorId 
    }.books