0

我有以下域类:

class Team{   
    static hasMany = [localMatches: Match, visitingMatches: Match]
    static mappedBy = [localMatches: 'localTeam', visitingMatches: 'visitingTeam']

    List<Match> localMatches = new ArrayList<Match>()
    List<Match> visitingMatches = new ArrayList<Match>()
}

class Match{
    Team localTeam
    Team visitingTeam
}

当我运行以下命令时:

Match match = new Match(localTeam: aLocalTeam, visitingTeam: aVisitingTeam)
match.save(flush: true, failOnError: true)

我收到异常“原因:org.h2.jdbc.JdbcSQLException:列“LOCAL_TEAM_ID”不允许使用 NULL;SQL 语句:”

所以我需要在保存比赛之前在每个团队中设置比赛以避免异常:

Match match = new Match(localTeam: aLocalTeam, visitingTeam: aVisitingTeam)
aLocalTeam.localMatches.add(match)
aVisitingTeam.localMatches.add(match)
match.save(flush: true, failOnError: true)

有什么方法可以映射类,所以我不需要在保存之前将匹配添加到每个团队?

4

1 回答 1

2

hasMany块定义 aMatch有 many localMatches,但随后在下面重新定义localMatches为与 single 的关系Match。我相信你的真正意思是:

class Team {   
    static hasMany = [localMatches: Match, visitingMatches: Match]
    static mappedBy = [localMatches: 'localTeam', visitingMatches: 'visitingTeam']
}

class Match {
    Team localTeam
    Team visitingTeam
}

以这种方式映射,Team将有两个集合,Matches每个集合Match都有一个localvisiting Team

于 2012-12-12T04:49:41.633 回答