我有以下内容:
class Match{
Team localTeam
Team visitingTeam
}
class Team{
static hasMany = [matches: Match]
}
抛出: 加载插件管理器时出错:类[class myapp.Team]中的属性[matches]是一对一的双向,在反面有两个可能的属性。可以在关系[team]的另一侧命名其中一个属性,也可以使用'mappedBy'静态来定义关系映射的属性。示例:static mappedBy = [matches:'myprop']
所以,我使用'mappedBy':
class Team{
static hasMany = [matches: Match]
static mappedBy = [matches: localTeam, matches: visitingTeam]
}
但是,通过这样做,当我从db获得团队时,匹配Set仅包含团队作为访问团队的匹配,这意味着它只将匹配映射到visitedTeam。
如果我编码de follow:
class Team{
static hasMany = [matches: Match]
static mappedBy = [matches: localTeam]
}
它只映射localTeam的匹配。
有没有办法将两个匹配(当团队是本地的,何时是访问者)映射到团队?
答案 0 :(得分:4)
请先阅读有关GORM绩效问题的文章:https://mrpaulwoods.wordpress.com/2011/02/07/implementing-burt-beckwiths-gorm-performance-no-collections
这可能就是你要找的东西:
class Team {
String name
String description
static constraints = {
name blank: false, nullable: false
description blank: true, nullable: true
}
static mapping = {
description type: 'text'
}
Set<Match> getHomeMatches() {
Match.findAllByHomeTeam(this).collect { it.homeTeam } as Set
}
Set<Match> getMatches() {
Match.findAllByTeam(this).collect { it.team } as Set
}
}
class Match {
Team homeTeam
Team team
static constraints = {
homeTeam nullable: false
team nullable: false
}
static mapping = {
id composite: ['homeTeam', 'team']
}
}
答案 1 :(得分:0)
试试这个
class Team {
static hasMany = [localTeamMatches: Match, visitingMatches: Match]
static mappedBy = [localTeamMatches: "localTeam", visitingMatches: "visitingTeam"]
}