1

うまくいけば、これは答えやすいものになるでしょう。次の情報を持つ player という Grails のクラスを作成しました。

class Player {
 String steamId
 String name
 String portrait
 static hasMany = {playerStatistics:PlayerStatistics}
 static hasOne = {playerForumProfile:PlayerForumProfile}
}

明確にするために、Player オブジェクトは 1 つの PlayerForumProfile オブジェクトを持つことができますが、プレーヤー オブジェクトは常に PlayerForumProfile オブジェクトの前に作成されます。私の問題は、PlayerForumProfile クラスのコントローラー内の「hasOne」プロパティに関連付けられている playerForumProfile オブジェクトにアクセスすることです。私はこれを行うと仮定しました:

    def playerForumProfileInstance = new PlayerForumProfile()
    def playerInstance = Player.get(params.id)

    playerForumProfileInstance = playerInstance.playerForumProfile

これを実行すると、playerInstance オブジェクトに関連付けられた PlayerForumProfile オブジェクトが playerForumProfileInstance 変数に取り込まれますが、これを試みると、Grails はエラーをスローして、playerForumProfile のようなプロパティがないことを知らせます。そのような方法で hasOne プロパティのオブジェクトにアクセスすることは可能ですか、それとも何か他のことをする必要がありますか?

編集: Player クラスを変更して、playerForumProfile という変数を含め、PlayerForumProfileを編集して、belongsTo宣言を含めようとしましたが、アプリの実行時に null ポインター例外が発生し続けました。

編集:もう少し情報、私はゼロから新しいgrailsアプリを作成し、Grailsのドキュメントに表示される方法で関係を作成し、問題なく実行されたので、新しいアプリを開始してコピーする方が簡単かもしれないと考えています以上のファイル。

4

2 に答える 2

5

GORM には hasOne 機能があります: http://grails.org/doc/latest/ref/Domain%20Classes/hasOne.html

于 2010-08-02T07:37:45.980 に答える
3

この回答は、grails 2.X 以降では正しくありません。2009 年に最初に回答されたときは正しいものでした。

GORM には「hasOne」プロパティはありません。

static belongsTo = [playerForumProfile: PlayerForumProfile]

属性名の通常の型指定された定義だけです。

PlayerForumProfile playerForumProfile

詳細については、1 対 1 GORM のドキュメントを参照してください。

于 2009-07-22T06:00:12.980 に答える