Grails:所属する関係のある要素のリストを取得しますか?

StackOverflow https://stackoverflow.com/questions/20353837

  •  25-08-2022
  •  | 
  •  

質問

私が持っていると仮定します Person そしてa Status. 。もしも Status このようなものです:

class Status {
    String text
    Person author
}

現在のユーザーのメッセージリストを取得するために、このようなことをしたでしょう。

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

しかし、私の関係がある場合 Status このようなものですが、どうすればよいですか?

static belongsTo = [Person]

ご協力いただきありがとうございます。

役に立ちましたか?

解決

私はbelongstoにマップ表記を使用する傾向があるので、私はこのようにそれをします:

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