2

我有以下内容:

class Match{ 
    Team localTeam
    Team visitingTeam
}

class Team{
    static hasMany = [matches: Match]
}

抛出:加载插件管理器时出错:类 [class myapp.Team] 中的属性 [matches] 是双向一对多,反面有两个可能的属性。要么命名关系 [team] 另一侧的属性之一,要么使用“mappedBy”静态定义关系映射的属性。示例:静态 mappedBy = [matches:'myprop']

所以,我使用'mappedBy':

class Team{
    static hasMany = [matches: Match]
    static mappedBy = [matches: localTeam, matches: visitingTeam]
}

但是,通过这样做,当我从 db 获得一个团队时,matches Set 仅包含该团队是访问队的匹配项,这意味着它仅将匹配项映射到访问队。

如果我编写以下代码:

class Team{
    static hasMany = [matches: Match]
    static mappedBy = [matches: localTeam]
}

它仅映射 localTeam 的匹配项。

有没有办法将两场比赛(当球队是本地的,当它是访客时)映射到球队?

4

2 回答 2

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']
   }   
} 
于 2012-06-27T04:41:00.930 回答
0

尝试这个

class Team {
    static hasMany = [localTeamMatches: Match, visitingMatches: Match]
    static mappedBy = [localTeamMatches: "localTeam", visitingMatches: "visitingTeam"]
}
于 2012-06-27T04:48:16.953 回答