跟踪用户和游戏的域类

跟踪用户和游戏的域类

我正在尝试做似乎很简单的事情。我有一个User类,并且有一个Game类可以匹配两个User。就这么简单:

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]

我究竟做错了什么?

最佳答案

所以这可能是您想要的:

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)
    }
}

关于grails - 跟踪用户和游戏的域类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14529081/

10-10 17:24