Grails 如何创建包含对象属性的类的实例

Grails 如何创建包含对象属性的类的实例,grails,Grails,我有以下课程: class Team{ String name boolean club } class Result{ int goals1 int goals2 } class SingleMatch { Team team1 Team team2 Date startDate Result firstHalfResult Result secondHalfResult Result extraTimeRe

我有以下课程:

class Team{
    String name
    boolean club
}

class Result{
    int goals1
    int goals2
}


class SingleMatch {
    Team team1
    Team team2
    Date startDate
    Result firstHalfResult
    Result secondHalfResult
    Result extraTimeResult
    Result penaltyResult
}
我试图在BootStrap.groovy中实例化这些类。但是我不知道如何实例化SingleMatch类

Team _team1 = new Team(name: "Fiorentina",club: true) 
Team _team2 = new Team(name:"Juventus",club: true)

Result a = new Result(goals1: 1, goals2: 0) 
Result b = new Result(goals1: 0, goals2: 2) 
Result c = new Result(goals1: 1, goals2: 0) 
Result d = new Result(goals1: 5, goals2: 4)

SingleMatch match1 = new SingleMatch(team1: _team1, team2: _team2, startDate: new Date(), firstHalfResult: a, secondHalfResult: b, extraTimeResult: c, penaltyResult: d)
这是正确的方法吗?

先尝试保存

Team _team1 = new Team(name: "Fiorentina",club: true) 
Team _team2 = new Team(name:"Juventus",club: true)
_team1.save()

_team2 .save()


Result a = new Result(goals1: 1, goals2: 0) 
Result b = new Result(goals1: 0, goals2: 2) 
Result c = new Result(goals1: 1, goals2: 0) 
Result d = new Result(goals1: 5, goals2: 4)
a.save()

b.save()

c.save()

d.save()

SingleMatch match1 = new SingleMatch(team1: _team1, team2: _team2, startDate: new Date(), firstHalfResult: a, secondHalfResult: b, extraTimeResult: c, penaltyResult: d)

match1.save();

您尝试过的是否不起作用?可能会出现错误,因为尚未保存团队和结果对象。如果您保存了团队和结果对象,那么您应该没事了。也就是说,我可能会以不同的方式对数据建模,但每次只会出现一个问题。:)谢谢Gregg,这很有效!您将如何对数据建模?:)