【发布时间】:2012-10-02 00:03:21
【问题描述】:
我经常做错事并在 Grails 中获得未保存的瞬态实例异常。我无法粘贴代码,如果我粘贴了,你可能只会告诉我永远停止编程。但这是我需要帮助的一般情况:
我打电话给instanceOfMyComplicatedDomainClass.save(flush: true, failOnError: true),grails/hibernate 给了我无处不在的:
object references an unsaved transient instance - save the transient instance before flushing: soexample.SomeOtherDomainClass
鉴于我有多个具有相同类的属性,我可以做什么(除了理解我自己的代码)来查看哪个属性是问题?我想要一个我可以在 .save() 之前调用的方法,它将 println/log/whatever 导致所有愤怒的属性的 name 。我举个例子:
class MyComplicatedDomainClass {
SomeOtherDomainClass dataContents
SomeOtherDomainClass moreDataContents
static constraints = {
dataContents(nullable:true)
moreDataContents(nullable:true)
}
}
class SomeOtherDomainClass {
String randomData
}
...
def someRandomMethodAnywhere(){
def newComplicated = new MyComplicatedDomainClass()
def imUnsaved = new SomeOtherDomainClass(randomData:'lalala')
def imOK = new SomeOtherDomainClass(randomData:'lalala2').save()
newComplicated.dataContents = imUnsaved
newComplicated.moreDataContents = imOK
//At this point newComplicated references an unsaved transient, "imUnsaved". If I try to save newComplicated it will fail.
def badPropertyList = findUnsavedTransients(newComplicated)
assert badPropertyList.contains("dataContents") //So findUnsavedTransients method returns a list of the names of the properties referencing the unsaved transients
}
我将如何编写 findUnsavedTransients? Hibernate 中是否已经有任何方法可以做类似的事情?
我问的问题与我的其他问题不同,以列出所有未保存的瞬态:Get a list of all objects grails is planning to magically save
此外,我看到(并已阅读)其他约 15 个“休眠:未保存的瞬态...”问题,我要求提供通用解决方案来查看问题所在,而不是针对我的问题的具体解决方案在今天的特定 sn-p 中做错了。教人钓鱼……可以这么说。
【问题讨论】:
-
哇,就制作一个好的 SEO 永久链接而言,我真的把标题搞砸了
-
在调用 save() 后删除 failOnErrorrs 并打印 domainInstance.errors
-
我想了一秒钟我忽略了一些如此明显的东西,但它的信息是相同的,我仍然无法确定是哪个属性是问题所在。
-
尝试会话的
contains()。 stackoverflow.com/questions/8752519/…
标签: hibernate grails grails-orm