我有以下域类:
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)
有什么方法可以映射类,所以我不需要在保存之前将匹配添加到每个团队?