当我使用entity.validate()实体对象保留在数据库中时,validate()grails方法存在问题,但是在保存数据之前我需要验证多个对象。

def myAction = {
  def e1 = new Entity(params)
  def ne1 = new EntityTwo(params)

  // Here is the problem
  // If e1 is valid and ne1 is invalid, the e1 object is persisted on the DataBase
  // then I need that none object has saved, but it ocurred. Only if both are success
  // the transaction should be tried
  if(e1.validate() && ne1.validate()){
    e1.save()
    ne1.save()
    def entCombine = new EntityCombined()
    entCombine.entity = e1
    entCombine.entityTwo = ne1
    entCombine.save()
  }
}

我的问题是我不希望在两次验证成功之前都保存对象。

最佳答案

在任何实例上被检测为已更改/脏污时,不希望其自动持久保存调用:

if (e1.validate() && ne1.validate()){
   ...
}
else {
   e1.discard()
   ne1.discard()
}

09-25 16:16