Grails一对多addTo()不';不行?
我有以下领域:Grails一对多addTo()不';不行?,grails,gorm,one-to-many,Grails,Gorm,One To Many,我有以下领域: class ChildPlacementFile{ ..... static hasMany = [children : Client] static belongsTo = [father : Client, mother: Client] Client child ...other fields etc. } class Client{ String firstName String lastName ....etc
class ChildPlacementFile{
.....
static hasMany = [children : Client]
static belongsTo = [father : Client, mother: Client]
Client child
...other fields etc.
}
class Client{
String firstName
String lastName
....etc etc.
}
我的客户要求我在表单中允许多次保存。所以我创建了一个类似这样的服务
def saveChildren(ChildPlacementFile childPlacementFileInstance, params){
def childrenList = []
def errorList = []
params.rowId = [params.rowId].flatten()
params.child = [params.child].flatten()
params.rowId.eachWithIndex {child, i ->
Client childInstance = Client.get(params.child[i])
if(childInstance){
childrenList.add(childInstance)
childPlacementFileInstance?.addToChildren(childInstance).save()
}else{
errorList.add(childInstance)
}
}
[errors: errorList, children:childrenList]
}
当我尝试在控制器上检查时,子对象似乎保存在save()方法[childPlacementFileInstance。?children]上,但当它重定向到show()方法时,[childPlacementFileInstance。?children]不再显示,它是空的。为什么?欧欧欧
没有任何错误,也没有例外。所以我希望它能起作用,但它没有:(我猜save方法在某些约束条件下会失败。调用save Check
childPlacementFileInstance.hasErrors()
后进行检查。
如果您不想抛出显式异常,请使用参数调用方法
childPlacementFileInstance.save(failOnError:true)
我确实尝试调用failOnError:true,但它没有给我任何错误。下面是它所使用的方法行:save()->saveChildrenService()似乎保存了孩子->重定向到show()在控制器中,没有显示子对象(&children)。它只是空的。我对子对象的唯一约束是:children(nullable:true)。我希望子对象是可选的。我指的是客户端对象的约束。它们是级联保存,因此也可能失败。不过,您没有错误,所以我不知道。可能会发布您执行的完整代码,或者将其上载到某个地方,以便我检查。我没有为客户端和ChildPlacementFile指定任何级联约束。既然您提到了,可以e是时候该删除了。我假设业务规则是:1.只有当拥有该文件的客户(孩子)被删除时,才能删除ChildPlacementFile。虽然母亲、父亲和孩子不会被删除,但他们与该文件的关联会被终止。2.当ChildPlacementFile被删除时,客户(母亲、父亲、孩子和孩子)与之关联的文件不会被删除,但它们的关联会被删除。3.删除母亲/父亲/子女时,childPlacementFile应该仍然存在。这有点让人困惑,但您能帮我添加约束吗?