I'm trying to do something that seems so simple. I have a User class, and I have a Game class that matches up two Users. It's this simple:
class User {
String username
static hasMany = [games:Game]
}
class Game {
User player1
User player2
}
When I ran this, I got
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']
So I did some digging and found mappedBy and changed my code to:
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'
}
}
Now I get
Non-existent mapping property [gid] specified for property [games] in class [class User]
What am I doing wrong?
So here is probably what you want:
class User {
static mappedBy = [player1Games: 'player1', player2Games: 'player2']
static hasMany = [player1Games: Game, player2Games: Game]
static belongsTo = Game
}
class Game {
User player1
User player2
}
Edit for new rules:
class User {
static hasMany = [ games: Game ]
static belongsTo = Game
}
class Game {
static hasMany = [ players: User ]
static constraints = {
players(maxSize: 2)
}
}