【问题标题】:3unsaved transient instance when getting an object in grails3在grails中获取对象时未保存的瞬态实例
【发布时间】:2014-08-20 22:50:55
【问题描述】:

获取对象时出现未保存的瞬态实例异常。

代码如下:

  String string = "name1|name2"
  string.split("[|]").each {
                           if (it) {
                                    println it
                                    Product p = Product.findByName(it.toUpperCase()).save()
                                    if(p) {
                                        Components c =  new Components(product: p).save(failOnError: true, flush: true)
                                        st.addToComponents(c)
                                    }

                                }
                            }

这里是例外

 | Error org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: Product; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: Product

我不明白获取对象是如何导致这种情况的。我已经检查过该对象不在数据库中。但是我的 FailOnError:true 在保存时没有给出任何错误。

【问题讨论】:

  • 你能添加更多代码吗?此代码是在控制器中还是在服务中执行的?它是什么”?您是否在迭代域对象列表?

标签: grails


【解决方案1】:
Product p = Product.findByName(it.toUpperCase()).save()

没有意义。尝试删除save()

Product p = Product.findByName(it.toUpperCase())

另外,尝试在 save() 那个实例之前执行 c.validate()。它可能有验证错误

【讨论】:

  • 那是为了测试,原来它没有 ,save() 并给出了同样的错误。
  • 在保存之前尝试验证()你的“c”实例
  • 似乎是休眠会话没有将批处理刷新到数据库的问题。这使得这篇文章变得多余。所以我又开了一个贴。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
相关资源
最近更新 更多