我正在尝试做一些看起来很简单的事情。我有一个用户类,我有一个匹配两个用户的游戏类。就是这么简单:
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]
我究竟做错了什么?