0

私はとても単純に見える何かをしようとしています。Userクラスがあり、2人のUsersに一致するGameクラスがあります。これは簡単です:

class User {
    String username
    static hasMany = [games:Game]
}

class Game {
    User player1
    User player2
}

これを実行すると、

Caused by GrailsDomainException: Property [games] in class [class User] is a bidirectional one-to-many with two possible properties on the inverse side. Either name one of the properties on other side of the relationship [user] or use the 'mappedBy' static to define the property that the relationship is mapped with. Example: static mappedBy = [games:'myprop']

そこで、掘り下げてmappedByを見つけ、コードを次のように変更しました。

class User {
    String username
    static hasMany = [games:Game]
    static mappedBy = [games:'gid']
}

class Game {
    User player1
    User player2
    static mapping = {
        id generator:'identity', name:'gid'
    }
}

今私は得る

Non-existent mapping property [gid] specified for property [games] in class [class User]

私は何が間違っているのですか?

4

1 に答える 1

3

だからここにおそらくあなたが望むものがあります:

class User {

    static mappedBy = [player1Games: 'player1', player2Games: 'player2']

    static hasMany = [player1Games: Game, player2Games: Game]

    static belongsTo = Game
}

class Game {
    User player1
    User player2
}

新しいルールを編集します。

class User {
    static hasMany = [ games: Game ]
    static belongsTo = Game
}

class Game {
    static hasMany = [ players: User ]
    static constraints = {
        players(maxSize: 2)
    }
}
于 2013-01-25T20:51:58.580 に答える