【发布时间】: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