문제

내가 a가 있다고 가정합니다 Person 그리고 a Status. 만약에 Status 이것과 같다 :

class Status {
    String text
    Person author
}

현재 사용자의 메시지 목록을 얻기 위해 이와 같은 작업을 수행했을 것입니다.

def messages = Status.withCriteria {
    author { 
        eq 'username', currentPerson.username
    }
}

하지만 내 관계가 있다면 Status 이렇게, 어떻게 그렇게 할 수 있습니까?

static belongsTo = [Person]

당신의 도움을 주셔서 감사합니다.

도움이 되었습니까?

해결책

SOLFITSTO에 대한지도 표기법을 사용하는 경향이 있으므로 다음과 같이 할 것입니다.

class Status {
   String text
   static belongsTo = [author: Person]
}

그런 다음 쿼리가 쉽습니다.

def messages = Status.findAllByAuthor(currentPerson)

Hasmany가있는 사람에게 양방향을 추가 한 경우 :

class Person {

   static hasMany = [messages: Status]
}

당신은 또한 이것을 할 수 있습니다 :

def messages = currentPerson.messages
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top