2013-05-11 3 views
0

I 도메인을HibernateCriteriaBuilder Grails의

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

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

I를 가질 수있는 유일한 입력 AuthorId이다. 이 값을 사용하여 HibernetCriteriaBuilder를 사용하여 도메인 도서 레코드를 얻는 방법 eq(propertyName, propertyValue)

감사합니다 !!!

답변

2

대답에, 몇 가지 체크 포인트 가기 전에 : - 도메인 클래스에 바인드 기본적으로
1. id ID를, 당신은 AuthorId이 필요하지 않을 수 있습니다.
2. Grails는 규칙을 준수합니다. 잘못된 상태를 피하려면 AuthorId 대신 authorId을 사용하고 싶습니다.

this page을 거쳤다면 지금까지 grails에서 criteria에 대한 기본 아이디어를 얻었을 것입니다. 그 외에도 criteriaassociations에서 사용할 수 있습니다. 당신이 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