我使用的是grails 2.5,我的域类,我创建了一个totomany关系:
class ActionPlanTemplate implements Serializable{
String name
static hasMany = [actionPlans: ActionPlan]
......
}
class ActionPlan implements Serializable{
String modelName
Date realStartDate
......
}
在我的控制器中,我将尝试将ActionPlanTemplate及其ActionPlan保存起来:
def save(ActionPlanTemplate apt) {
def method = "save"
def result, actionPlans = []
try {
ActionPlanTemplate.withTransaction {
println apt
apt.actionPlans.each{ ap ->
ap.realStartDate = new Date()
println "b" + ap.realStartDate
actionPlans.add(ap)
}
if (!apt.save(flush:true)) {
apt.errors.allErrors.each { println "i------" + it }
}
println apt.actionPlans
}
return redirect (controller:'actionPlanTemplate', action: 'index')
} catch (Exception e){println "e---------" + e}
}
保存后,在控制台中显示:
com.analysis.model.ActionPlanTemplate : (unsaved)
bFri Oct 05 09:49:08 CEST 2018
[com.analysis.model.ActionPlan : 102]
所以,我进入数据库,只看到ActionPlanTemplate,但没有出现ActionPlan,但是在控制台中:“ com.analysis.model.ActionPlan:102”