Grails数据绑定疑问
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 没有保存。我无法弄清楚。任何帮助将不胜感激。
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()
}
The CarroInstance is saving, but the carroMovelInstance isn't. I cannot figure it out. Any help would be apreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Grooveek 是正确的,因为您从未调用过
carroMovelInstance.save( )
。然而,对您来说,简单地利用 Grails 的数据绑定可能会更简单,而不是不必要地创建关联并手动绑定参数。
阅读 Grails 数据绑定,特别是关联部分。此外,请阅读"了解级联更新和删除" 了解如何对父域对象调用
save()
将(或不会)级联到关联的域对象。Grooveek is correct in that you haven't ever invoked
carroMovelInstance.save()
.However, it might be simpler for you to simply take advantage of Grails' databinding, instead of unnecessarily creating the associations and manually binding the parameters.
Read up on Grails Data Binding, particularly the parts about associations. Additionally, read "Understanding Cascading Updates and Deletes" to understand how a call to
save()
on a parent domain object will (or will not) cascade to an associated domain object.你从不要求 carroMovelInstance 保存... carro 实例也没有引用 carroMovel 实例,因此没有级联保存
you never ask for the carroMovelInstance to save... The carro instance has nor reference to carroMovel instance so there is no cascading of saving