Grails在保存和刷新时不生成标识 [英] Grails no id generated when saved and flushed

查看:92
本文介绍了Grails在保存和刷新时不生成标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Grails不会为数据库对象生成id。我用相应的域类创建了对象。现在如果我调用save(flush:true),我会得到空id ...



但是不应该使用flush来生成id?

解决方案

你确定它确实保存了它,这通常意味着存在错误,所以你需要捕获它并将其中继或返回。
你应该能够通过执行

  if(!object。save(flush:true))来查看错误{
//抛出新的RuntimeException(出错了);
// println对象错误$ {object.errors} //.allErrors}
//或
//object.errors.reject('object.failed.saved','' )
}

有时甚至会出现更多的嵌套失败, p>

  try {
subobject.removeFromElements(object)
subobject.save(flush:true)
} catch(Exception e){
throw new ValidationException('failed.message',subobject.errors)
}

如果你首先尝试第一个方法来查看是否有错误,那么finally还没有尝试作为一个测试,通过在整个保存块中包装try catch来查看其他组件是否引发你的问题。你回答

Grails is not generating id for database object. I created object with corresponding domain class. Now if I call save(flush:true) i get null id in ...

But shouldnt the flush generate the id ?

解决方案

Are you sure it has really saved it, this typically means there were errors so you need to capture it and relay it back or whatever. you should be able to see the errors by doing

if (!object. save(flush:true)) {
  // throw new RuntimeException("something went wrong");
   //println "object errors ${object.errors} //.allErrors}
   //or 
   //object.errors.reject('object.failed.saved', '')
}

Sometimes it is even more nested failure and can be caught like this

try {
            subobject.removeFromElements(object)
            subobject.save(flush:true)
        } catch (Exception e) {
            throw new ValidationException('failed.message',subobject.errors)
        }

If you try first method first to see if errors the finally if still nothing try as a test by wrapping a try catch around entire save block to see if other components are causing your issue. Between that lies you answer

这篇关于Grails在保存和刷新时不生成标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆