Class Carro {
String name
String marca
String matricula
}
Class CarroMovel{
String pro1
String prop2
String prop3
Carro carro
static hasMany = [ carros: Carro]
}
def save2 = {
def carroInstance = new Carro()
def carroMovelInstance = new CarroMovel()
carroInstance.name = params.name
carroInstance.marca = params.marca
carroInstance.matricula = params.matricula
carroMovelInstance.prop1 = params.carroMovel.prop1
carroMovelInstance.prop2 = params.carroMovel.prop2
carroMovelInstance.prop3 = params.carroMovel.prop3
carroInstance.save()
carroMovelInstance.carro = carroInstance
carroMovelInstance.save()
}
CarroInstance正在保存,但carroMovelInstance未保存。我想不明白。任何帮助将不胜感激。
最佳答案
您可能有一个验证错误。尝试在其上调用validate()
并检查错误。或者,尝试使用carroMovelInstance.save(failOnError: true)
保存它,如果它不通过验证,则会得到异常。